Goal c "Did you forget to lay out alloc and init?"

I'm just starting to climb the Objective-C learning curve (using the Nerd Ranch iOS programming book).

Based on what I know from other languages ​​about the "nesting" of several executions on the same line, I assumed that I could change:

NSString* descriptionString = [[NSString alloc] initWithFormat:@"%@", possesionName] 

with a two-line version:

NSString* descriptionString = [NSString alloc];
[descriptionString initWithFormat:@"%@", possesionName] 

but it seems that the second attempt throws an exception

2012-01-22 18:25:09.753 RandomPossessions[4183:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -length only defined for abstract class.  Define -[NSPlaceholderString length]!'

Can someone help me understand what exactly I am doing wrong here? Thank you very much in advance.

PS. If this is the way of reporting Objective-C, and you have to do alloc and init on the same line, just let me know - I suggested that this is just a set of functions that can either be executed two at a time, or one after the other.

+5
source share
3

alloc . init . nil. self = [super init] init.

NSString - , .

, , , ARC - . , - , , , . , , , .

NSString . . , , factory "" - , , initWithFormat:. , alloc/init , , , , , - .

, objective-c - , , . , , . - undefined, , , . , , , , "" , , , , , . , , .

copy, NSString, . , NSString , - - , , . , , , copy , , , .

"" - , Objective-C, " ", , .

... objective-c , 10 , 1 . , .

, Xcode " " 120 . , . , , .

.:)

+3

( ) , initWithFormat descriptionString, alloc .

NSString* descriptionString = [NSString alloc];
descriptionString = [descriptionString initWithFormat:@"%@", possesionName] 

. , , alloc, , - init, init - .

+9

Apple, initWithFormat:

NSString, Unicode .

, :

NSString* descriptionString = [NSString alloc];
descriptionString = [descriptionString initWithFormat:@"%@", possesionName];

For more information, go to: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/ initWithFormat :

0
source

All Articles