Advantage of data type identifier versus NSString in Objective-C?

This code ...

NSString * s = [[NSString alloc] initWithString:@"Hello, World"]; 
s = s.lowercaseString;
NSLog(@"%@", s);

... allows you to use dotted notation, but is very typed. This code ...

id s = [[NSString alloc] initWithString:@"Hello, World"]; 
s = [s lowercaseString];
NSLog(@"%@", s);

... is weakly typed and requires the use of square brackets.

Besides this, is there any advantage to using one over the other?

+3
source share
3 answers

If you create NSString, you can also declare it as NSString, and let the compiler help you.

id , . , IBAction id, .

:

, ,

-, @"Hello, World", NSString, . initWithString, . :

NSString *s = @"Hello, World";

, alloc, .

-, s.lowerCaseString. , . , , , .

+12

. , NSString , NSNumber.

, .

+5

, . / , lowercaseString - .

, , - . , , [a loercaseString], .

Of course you can use id, but your example is not one of them

+4
source

Source: https://habr.com/ru/post/1720479/


All Articles