ObjectC ---- dictionary class and collection class and array sorting in quick enumeration and OC

Source: Internet
Author: User
Tags allkeys new set

ObjectC ---- dictionary class and collection class and array sorting in quick enumeration and OC
 

 

 

// Immutable dictionary

// The dictionary is a set used to store key-value pairs. The elements must be of the object type.

 

// The dictionary is unordered.

 

// Dictionary assignment

NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys: @ "guozai", @ "name", @ "nan", @ "sex", @ "14", @ "age ", nil];

// Print the dictionary name directly.

NSLog (@ "% @", dic );

 

// The obtained key values all point to NSString *.

NSString * age = [dic objectForKey: @ "age"];

NSLog (@ "% @", age );

NSString * nam = [dic objectForKey: @ "name"];

NSLog (@ "% @", nam );

// NSString * sex = [dic objectForKey: @ "sex"];

NSLog (@ "% @", [dic objectForKey: @ "sex"]);

 

// Obtain all key values

NSArray * keys = [dic allKeys];

// Print the array name directly

NSLog (@ "% @", keys );

 

// Obtain all value values

NSArray * objects = [dic allValues];

NSLog (@ "% @", objects );

 

 

 

// ================================================ ==============================

// Variable dictionary

 

// Create a new dictionary based on the existing dictionary

NSMutableDictionary * mulDic = [NSMutableDictionary dictionaryWithDictionary: dic];

 

// Add a key-Value Pair score-> 100

[MulDic setObject: @ "100" forKey: @ "score"];

NSLog (@ "% @", mulDic );

 

// Delete the key-value pair sex-> nan

[MulDic removeObjectForKey: @ "sex"];

NSLog (@ "% @", mulDic );

 

// Modify the value of the key-value pair, which is actually the setting of the added method.

[MulDic setObject: @ "13" forKey: @ "age"];

NSLog (@ "% @", mulDic );

// ================================================ ============

// Traverse the dictionary

 

NSArray * allkeys = [mulDic allKeys];

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

// Obtain the key corresponding to the subscript

NSString * key = [allkeys objectAtIndex: I];

// Obtain value based on key

NSString * value = [mulDic objectForKey: key];

NSLog (@ "% @", value );

}

 

// Or use the allvalue method to retrieve all values.

NSArray * allValue = [mulDic allValues];

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

NSLog (@ "% @", [allValue objectAtIndex: I]);

}

 

/*

1. The dictionary is a set used to store key-value pairs. The elements in the dictionary must be of the object type,

2. the keys in the dictionary are unique and cannot be repeated, but the values can be repeated.

3. The dictionary is an unordered set. The key-value pairs are not ordered. You can understand the dictionary as "subscript is an array of strings"

4. The dictionary uses the key to remove values, set values, and modify values. This allows you to conveniently retrieve all keys, traverse values based on key values, or directly traverse all values using the allvalue method.

*/

 

 

// ================================================ ======================================

// NSSet set

 

// Use the constructor to define the set

NSSet * set = [NSSet setWithObjects: @ "guozai", @ "zaigguo", @ "guoguo", nil];

// Use the initialization method to define the set

NSSet * set2 = [[NSSet alloc] initWithObjects: @ "guozai", @ "zaiguo", @ "guoguo", nil];

 

NSLog (@ "% @, % @", set, set2 );

 

// Obtain all elements and print them

NSArray * sets = [set allObjects];

NSLog (@ "% @", sets );

 

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

NSLog (@ "% @", [sets objectAtIndex: I]);

}

 

// Obtain any element in the set

 

NSString * str = [set anyObject]; // The anyObject method is not necessarily random and is generally the first element.

NSLog (@ "% @", str );

 

// Determine whether the set contains an element

BOOL re = [set containsObject: @ "guozai"];

If (re ){

NSLog (@ "baohan ");

}

Else {

NSLog (@ "bubaohan ");

}

 

// ================================================ ============================

// NSMutableSet

 

// Create a new set based on the existing set combination

NSMutableSet * mulSet = [NSMutableSet setWithSet: set];

 

// Add Element

[MulSet addObject: @ "zaizai"];

NSLog (@ "% @", mulSet );

 

// Delete an element

[MulSet removeObject: @ "zaizai"];

NSLog (@ "% @", mulSet );

 

// ================================================ ==================

 

// NSCountedSet is a subclass of NSMutableSet and can be used to calculate the number of repetitions.

NSCountedSet * countedSet = [NSCountedSet setWithSet: mulSet];

[CountedSet addObject: @ "guozai"];

NSUInteger count = [countedSet countForObject: @ "guozai"];

NSLog (@ "% ld", count );

 

// ================================================ ==================

// When you use Quick enumeration to traverse elements in a set, you cannot add or delete elements in the set. Otherwise, the system will crash.

 

// Fast enumeration of Arrays

NSMutableArray * arr = [NSMutableArray arrayWithObjects: @ "1", @ "2", @ "3", nil];

For (NSString * str in arr ){

NSLog (@ "% @", str );

// Cannot add or delete

}

 

// Fast dictionary Enumeration

NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithObjectsAndKeys: @ "guozai", @ "name", @ "man", @ "sex", @ "19", @ "age ", nil];

 

// When the dictionary is quickly enumerated to traverse the dictionary, the dictionary key is traversed;

For (NSString * str in dict ){

// NSString * value = [dict objectForKey: str];

// NSLog (@ "% @", value );

NSLog (@ "% @", str); // key of the output

}

 

// Fast enumeration of Sets

NSMutableSet * mulSet2 = [NSMutableSet setWithObjects: @ "1", @ "2", nil];

For (NSString * str in mulSet2 ){

NSLog (@ "% @", str );

}

 

 

 

// ================================================ ========================

 

// SortedArrayUsingSelector creates a new array without affecting the elements of the original array.

// SortUsingSelector does not create a new array, but adjusts the element position based on the original array.

 

 

// Define the comparison method, for example, the compareByPrice method defined in the class below

 

-(NSComparisonResult) compareByPrice :( Student1 *) stu

{

// NSOrderedDescending = 1 is returned for exchange. The other two are returned for not-exchanged.

 

If (_ price> [stu price]) {

Return NSOrderedAscending; // =-1

}

Else if (_ price = [stu price])

{

Return NSOrderedSame; // = 0

}

Else {

Return NSOrderedDescending;

}

 

}


 

 

NSArray * sortedArray = [students sortedArrayUsingSelector:

@ Selector (compareByPrice :)]; // method name in parentheses

 

// Sort in the original Array Using the sortUsingSelector @ selector () method

 

For (Student1 * stu in sortedArray ){

NSLog (@ "% @", stu );

}

 

 

// Step 1: Define the comparison method in the model class

// Step 2: Send a sort message to the array

// The default value is ascending.

========================================================== ==========================================================

The description method of NSLog is redefined to achieve the desired output when the object is printed.

 

// When printing an object, the description method of the object is called.

-(NSString *) description

{

 

NSString * str = [NSString stringWithFormat: @ "name = % @, age = % d, score = %. 2f", _ name, _ age, _ score];

Return str;

}


 

 

 

 

 

 

 

 

 

 

 


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.