cIASSMAte
pate
Page
Unit -3
NS amas andNS oliction ars
NSAra
NSArouy is a general purpo se. array ype.
4 represents an oroed collechion of objects
ike NSSet, NSArrauy is immutablk, S0 you cannot
clynami cally add or remove item s.
Smmutable aays Can be
dafinad as literals using the
OC] syntax
key value.
NSSet
NSArray NSDichio nany
NSAray "gumanMakes = @"Mercedes - Bene"
BMw " "Porsche" Volks wagen"
Audi"J;
NSLog (aFirst german make i Yo@QumanMake lO
Output First qurman make : Merceole - Benz
Example of NaAmoy Lwith Objcts
arry
NSAmay KONAS wkMakes=NSArau With0bjects
"Lotus", "Jagquar",@ Butly nil];
NSLog @"First Uk Make:" %@vkMakes
object At Inolex : 0])
Output Frst uk Mako i kotus
Fast Enumeration - NSArray
Fast-enumerationis the most efficient way to
ituate an NSAray
The oontnts oue guaranteed to appear in the correct
orolen
NSArra * qrmanMaku = "Mercedes - Benz"
"BMW',"Porsche'"
Volkswagen",
with FastéNumration
for (Nssting *item in germanMakes)
NSLog CQ"/Q" iten)
l wih Traolitional for loop
for int ieo , i< gurmanMaked count]; i)
NSLog ("%Q germanMakea li]);
3
page
There are Seveual advant age to Fast Enumerathon ’
St is co nhiolerably more efficient than
traditional for
Syntox is more conoise
Enumerotio n is "sofe'" as it has a mutathion
quarol.
Sf you attempt to modify the collechion
during numurahon, exception will be raised
NSDictionary
represents n Unoroeed oollection of objects
They aociate Cach alve with a key which acts
a a label for the value.
NSDichon ary is immutable
but the NSMutable Dictionars data structure allous
you to cynamically add and remove enties_A
necesany
NS Diotion ary can be defined using the literal
Page
eg.itural syntax
NSDichionary. *inventoy = A [NSNumber
numbwlith Int:1]"B"NSNum ber
mberith Int : ], @ c" :NSNumber
hod number with Tnt : 31, a "0" NS Number
numburlUith Int s 4]
/Nalues and keys ad aguements
dichonuy
NSDietionay * invntory = [ [ NSDichionary, With Objccts Andkeys:
INSNumbe numberwith Int :1]_ "A
NS Numbnumbr Wih Int : R,@
NSNumber numberlwith Int : 3 , c "
NSNumber numberlwith Int : 4],@" D", ni];
Valus and kays amays
NSAray * letters = l@"A"_,"g","c", QD7:
NSArray * numbus = LNSNumber nunmberwithInt :17,
NSNumbe numbvlWith Int : a,NSNugber
numberwithTnt3NSNumbe
: numbeWithInt: 4]:
inventory= [NSDiction cury dieionay WithObject s letters
forkuys : numbers]
NSkog ( " inventory)
Fast Enumerahon
Page
Fast- enwmuration is the most efficient way to
ehumerate a dlichion any it loops through the
keys (not he value)
NSDictionany also olefne aoountmethod which
retms the no. of entriea inthe collection.
cg. or (id keuy in inventory)
NSLog (@%Q o inventony (ky], key);