objective c - Why can't I create an NSMutableArray of integers? -
is format not allowed in objective c?
nsmutablearray arraya=[0,1,2,3,4,5];
is way adding 1 integer @ time?
there 2 parts question:
integers value-types , nsmutablearray can have object-types in it.
as comment said, there's magic syntax:
@1
which says "i know primitive, please oh mighty compiler auto-magically make object me."
similarly, there's syntax:
@[ ... ]
which converts c-array objective-c nsarray [subclass].
so this:
nsarray * array = @[ @1 , @2 , @3 , ... ] ;
which give array of numbers. you'd have use them though:
for ( nsnumber * number in array ) { nslog ( @"this value %i" , (int)number.intvalue ) ; // alternatively, exact same thing: nslog ( @"this value %i" , (int)[number intvalue] ) ; }
if wanted make mutable:
nsmutablearray * array = [ @[@1,@2,@3] mutablecopy ] ;
other languages (like python) have syntax want built-in because own language , can internally represent object-type or value-type. don't have distinguish between them, , pay cost in terms of optimizability.
objective-c based on c , must still conform c. furthermore, syntax must still conform c, , number , [] tokens have specific meanings in c , c++ can't re-define them. therefore, used favorite syntax: '@' sign, patterning after strings.
char const * str = "abc" ; nsstring * str = @"abc" ; int = 1 ; nsnumber * = @1 ; int arr[] = { 1 , 2 , 3 } ; nsarray * arr = @[ @1 , @2 , @3 ] ; // note: there's no concept of dictionary in c nsdictionary * dict = @{ @"a":@1 , @"b":@2 } ;
this called "boxing" because you're boxing primitive object.
note inner part number doesn't have literal:
int = 5 ; nsnumber * ii = @(i) ; char const* str = "abc" ; nsstring * string = @(str) ;
see more information: http://clang.llvm.org/docs/objectivecliterals.html
Comments
Post a Comment