//
// main.m
// 存储集合
//
// Created by wu jianhua on 16-8-3.
// Copyright (c) 2016年 wujianhua. All rights reserved.
//
#import <Foundation/Foundation.h>
void testNSSet()
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSSet *set = [[NSSet alloc]
initWithObjects:@"string1", @"string2",@"string3",nil];
NSArray *setArray = [set allObjects];
NSLog(@"The objects in set are %@",setArray);
NSMutableSet *mutableSet = [[NSMutableSet alloc]init];
[mutableSet addObject:@"string1"];
setArray = [mutableSet allObjects];
NSLog(@"The objects in mutableSet are %@",setArray);
[pool drain];
}
void testNSDictionary()
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
@"string1",@"key1", @"string2",@"key2",@"string3",@"key3",nil];
NSString *string1 = [dictionary objectForKey:@"key1"];
NSLog(@"The object for key, key1 in dictionary is %@",string1);
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc]init];
[mutableDictionary setValue:@"string" forKey:@"key1"];
string1 = [mutableDictionary objectForKey:@"key1"];
NSLog(@"The object for key, key1 in mutableDictionary is %@",string1);
[pool drain];
}
void testNSArray()
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *array = [[NSArray alloc]initWithObjects:@"string1", @"string2",@"string3",nil];
NSString *string1 = [array objectAtIndex:0];
NSLog(@"The object in array at Index 0 is %@",string1);
NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
[mutableArray addObject: @"string"];
string1 = [mutableArray objectAtIndex:0];
NSLog(@"The object in mutableArray at Index 0 is %@",string1);
[pool drain];
}
//http://www.yiibai.com/objective_c/objective_c_data_storage.html
int main(int argc, const char * argv[])
{
testNSArray();
testNSDictionary();
testNSSet();
return 0;
}
objective-c之存储集合
最新推荐文章于 2025-07-23 10:21:05 发布
本文通过示例介绍了Objective-C中三种主要的集合类型:NSArray、NSDictionary和NSSet的基本用法,包括创建、读取集合中的元素等操作。

544

被折叠的 条评论
为什么被折叠?



