For .. in traversal, enumerator, for .. in traversal Enumeration

Source: Internet
Author: User

For .. in traversal, enumerator, for .. in traversal Enumeration

# Pragma mark ------------ for looping through elements in the Set ------

// Create an array containing five string objects. Retrieve all elements in the array in reverse order and store them in another variable array.

NSArray * array = @ [@ "1", @ "2", @ "3", @ "4", @ "5"];

NSMutableArray * marray = [NSMutableArray arrayWithCapacity: 0];

For (NSInteger I = [array count]-1; I> = 0; I --){

[Marray addObject: array [I];

}

NSLog (@ "marray is % @", marray );

NSDictionary * dic =@ {@ "name": @ "zhonger", @ "sex": @ "m", @ "age": @ "29 "};

NSArray * key = [dic allKeys];

NSArray * value = [dic allValues];

For (int I = 0; I <[key count]; I ++ ){

// NSLog (@ "% @ = % @", key [I], value [I]);

// NSLog (@ "% @", [dic objectForKey: key [I]);

NSLog (@ "% @", dic [key [I]);

// How does the array access elements or objects? ObjectAtIndex:

// What is the syntactic sugar of the element accessed by the array? Array object [subscript]

// How does the dictionary access the value? Object ForKey:

// What is the syntactic sugar of the dictionary access value? Dictionary object [key]

}

// Create a group, save three pieces of information, and traverse each piece of information.

NSSet * set = [NSSet setWithObjects: @ "1", @ "888888", @ "3", nil];

NSArray * allSet = [set allObjects];

For (int I = 0; I <[allSet count]; I ++ ){

NSLog (@ "allSet is % @", allSet [I]);

}

 

# Pragma mark ------------ NSEnumerator ------

NSArray * enumArray = @ [@ "jinkangda", @ "zhubada", @ "dazi", @ "bada"];

// Create an enumerator attached to an array

NSEnumerator * rator = [enumArray objectEnumerator]; // forward

// Create an object

Id object = nil; // The capacity is empty.

While (object = [rator nextObject]) {

NSLog (@ "Forward = % @", object );

}

NSEnumerator * rator1 = [enumArray reverseObjectEnumerator];

Id object1 = nil;

While (object1 = [rator1 nextObject]) {

NSLog (@ "Reverse Order =%@", object1 );

}

// Dictionary enumeration Traversal

NSDictionary * enumDic =

@ {@ "Key1": @ {@ "key1": @ "value1 "},

@ "Key2": @ {@ "key2": @ "value2 "},

@ "Key3": @ {@ "key3": @ "value3 "}};

NSEnumerator * dicRator = [enumDic objectEnumerator];

Id value1 = nil;

While (value1 = [dicRator nextObject]) {

NSLog (@ "value1 is % @", value1 );

}

# Pragma mark ------------ for... in... quick enumeration ------

// Type * object indicates that the object type obtained from the collection is only a name. You only need to satisfy the variable naming rules.

// Collection indicates the set to be traversed.

// Operations to be performed in statements traversal, such as using enumerated objects to implement certain functions

/*

For (<# type * object #> in <# collection #> ){

<# Statements #>

}*/

For (id object in enumArray ){

NSLog (@ "----- % @", object );

}

// Use forin traversal to obtain each value in the dictionary

// Traverse the key in the dictionary and obtain the value of each key.

For (id key in enumDic ){

// NSLog (@ "% @", [enumDic objectForKey: key]);

NSLog (@ "% @", enumDic [key]);

For (id key1 in enumDic [key]) {

NSLog (@ "% @", enumDic [key] [key1]);

// NSLog (@ "% @", [[enumDic objectForKey: key] objectForKey: key1]);

}

}

// Define the delimiter array, which contains five string objects. Extract all the character strings in the array and splice the delimiter to generate a new character string.

NSArray * ar = @ [@ "I", @ "L", @ "o", @ "v", @ "eyou"];

NSMutableString * mstr = [[NSMutableString alloc] initWithCapacity: 0];

For (id object in ar ){

// [Mstr stringByAppendingString: object];

[Mstr appendString: object];

}

NSLog (@ "% @", mstr );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.