[Objective-c] 010_foundation framework of Nsset and Nsmutableset

Source: Internet
Author: User



In the Cocoa Foundation, Nsset and Nsmutableset, like the nsarray functional nature, are used to store objects that belong to a collection. However, Nsset and Nsmutableset are unordered, guaranteeing the uniqueness of the data and will not have any effect when inserting the same data.



Nsset initialization and common operations


#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

-(BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {

NSSet * students = [NSSet setWithObjects: @ "xiao ming", @ "xiao hui", @ "da xiong", nil];
NSSet * teachers = [[NSSet alloc] initWithObjects: @ "principal", @ "Vice-Chancellor", @ "Director of politics and education", nil];
NSArray * array = [NSArray arrayWithObjects: @ "xiao ming", @ "xiao hui", @ "da xiong", @ "xiao li", nil];
NSSet * students_2 = [NSSet setWithArray: array];

NSLog (@ "students:% @", students);
NSLog (@ "teachers:% @", teachers);
NSLog (@ "students_2:% @", students_2);

// Get the number of objects contained in the collection students
NSLog (@ "students count:% lu", (unsigned long) students.count);

^ // Get all objects in the collection teachers as an array
NSArray * allTeacher = [teachers allObjects];
NSLog (@ "allObj:% @", allTeacher);

// Get any object in teachers
NSLog (@ "anyObj:% @", [teachers anyObject]);

^ // teachers contains an object
If ([teachers containsObject: @ "Vice principal"]) {
NSLog (@ "teachers has a vice principal");
}

^ // Whether contains the object in the specified set
If ([students_2 intersectsSet: students]) {
NSLog (@ "intersects");
}

Http: // Whether it exactly matches
If ([students_2 isEqualToSet: students]) {
NSLog (@ "exact match");
} Else {
NSLog (@ "exact match? NO ...");
}

^ /// Whether it is a sub-collection
(If [students isSubsetOfSet: students_2]) {
NSLog (@ "students isSubsetOf students_2");
}

Http: // Iterate through
NSEnumerator * enumerator = [teachers objectEnumerator];
NSObject * teacher = [enumerator nextObject];
While (teacher! = Nil) {
NSLog (Data in @ "teachers:% @", teacher);
Teacher = [enumerator nextObject];
}

^ // Fast enumeration
For (NSObject * teacher in teachers) {
NSLog (Data in @ "teachers:% @", teacher);
}

Return YES;
}

@end





Nsmutableset initialization and common operations


#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

-(BOOL) application: (UIApplication *) application didFinishLaunchingWithOptions: (NSDictionary *) launchOptions {
    
    NSMutableSet * mutableStudent = [NSMutableSet setWithObjects: @ "F1", @ "F2", @ "F3", nil];
    NSMutableSet * mutableTeacher = [NSMutableSet setWithObjects: @ "B1", @ "B2", @ "B3", nil];
    NSMutableSet * mutableStudent2 = [NSMutableSet setWithObjects: @ "F1", @ "F2", @ "F3", @ "F4", nil];
    
    // Subtraction of collection elements
    [mutableStudent2 minusSet: mutableStudent];
    NSLog (@ "mutableStudent2 minus mutableStudent:% @", mutableStudent2);
    
    // mutableStudent2 leaves only equal elements
    [mutableStudent intersectSet: mutableStudent2];
    NSLog (@ "intersect:% @", mutableStudent2);
    
    // mutableStudent merge collection
    [mutableStudent unionSet: mutableStudent2];
    NSLog (@ "union:% @", mutableStudent);
    
    // mutableTeacher deletes the specified element
    [mutableTeacher removeObject: @ "好色 仙人"];
    NSLog (@ "removeObj:% @", mutableTeacher);
    
    // mutableTeacher deletes all data
    [mutableTeacher removeAllObjects];
    NSLog (@ "removeAll:% @", mutableTeacher);
    
    return YES;
}

@end 







This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4623082.html








[Objective-c] 010_foundation framework of Nsset and Nsmutableset


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.