Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to convert from char* to id* with ARC enabled
    text
    copied!<p>I'm trying to construct "fake" variable arguments list, using the technique described <a href="http://cocoawithlove.com/2009/05/variable-argument-lists-in-cocoa.html" rel="nofollow">here</a>, but for ARC-enabled project and I can't figure out how to get rid of the error I'm getting.</p> <p>Here's the code in question:</p> <pre><code>NSMutableArray* argumentsArray = [NSMutableArray array]; // ... Here I fill argumentsArray with some elements // And then, I want to construct a "fake" variable argument list char* fakeArgList = (char*) malloc( sizeof(NSString*) * [argumentsArray count]); [argumentsArray getObjects: (id*) fakeArgList]; NSString* content = [[NSString alloc] initWithFormat: formatString arguments:fakeArgList]; </code></pre> <p>XCode complains on the <em>(id</em>) fakeArgList* casting, saying:</p> <blockquote> <p>Cast of non-Objective-C pointer type 'char *' to '_autoreleasing id *' is disallowed with ARC</p> </blockquote> <p>My initial theory was that I just need to add __unsafe_unretained to (id*) casting to tell ARC that I'm responsible for that block of memory and it shouldn't retain/release it, but that doesn't work and I can't figure out how to fix this problem.</p> <p><strong>Update:</strong> Here's the full function. It should take a printf-style format string and a variable list of field names inside the .plist and output a formatted string with data loaded from .plist. I.e., if I have a .plist file with fields "field1" = "foo" and "field2" = 3 and I call <code>[loadStringFromFixture: @"?param1=%@&amp;param2=%d", @"field1", @field2]</code> then I should get string "?param1=foo&amp;param2=3"</p> <pre><code>- (NSString*) loadStringFromFixture:(NSString*) format, ... { NSString* path = [[NSBundle mainBundle] bundlePath]; NSString* finalPath = [path stringByAppendingPathComponent:@"MockAPI-Fixtures.plist"]; NSDictionary* plistData = [NSDictionary dictionaryWithContentsOfFile:finalPath]; va_list argumentsList; va_start(argumentsList, format); NSString* nextArgument; NSMutableArray* argumentsArray = [NSMutableArray array]; while((nextArgument = va_arg(argumentsList, NSString*))) { [argumentsArray addObject: [plistData objectForKey:nextArgument]]; } NSRange myRange = NSMakeRange(0, [argumentsArray count]); id* fakeArgList = (__bridge id *)malloc(sizeof(NSString *) * [argumentsArray count]); [argumentsArray getObjects:fakeArgList range:myRange]; NSString * content = [[NSString alloc] initWithFormat:formatString arguments:(__bridge va_list)fakeArgList]; free(fakeArgList); return content; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload