1.内存高级
新建一个Person类.
Person.h
@interface Person : NSObject
@property(nonatomic, retain)NSString *name;
@property(nonatomic, retain)NSString *sex;
@property(nonatomic, assign)NSUInteger age;
@property(nonatomic, retain)NSString *hobby;
@end
Person.m
- (void)dealloc
{
[_sex release];
[_name release];
[_hobby release];
[super dealloc];
}
- (void)setName:(NSString *)name {
if (_name != name) {
[_name release];
_name = [name retain];
[name retain];
[_name release];
_name = name;
}
}
- (void)setSex:(NSString *)sex {
if (_sex != sex) {
[_sex release];
_sex = [sex retain];
}
}
- (void)setAge:(NSUInteger)age {
_age = age;
}
- (void)setHobby:(NSString *)hobby {
if (_hobby != hobby) {
[_hobby release];
_hobby = [hobby retain];
}
}
@end
main.m
Person *person = [[Person alloc] init];
NSString *name = [[NSString alloc] initWithString:@"梅西"];
NSString *name1 = [[NSString alloc] initWithString:@"C.罗"];
[person setName:name];
[name release];
NSLog(@"%@", person.name);
(结果:梅西)
[person setName:name1];
[name1 release];
[person setName:name1];
NSLog(@"%@", person.name);
(结果:C.罗)
2.集合
(1).
NSSet *set1 = [NSSet setWithObjects:@"a", @"b", @"b",@"b", nil];
NSLog(@"%@", set1);
(2). 打印元素个数.
NSLog(@"%ld", set1.count);
(结果:2). // 应为元素集合必须唯一,重复的只打印一个.
(3). 获取某个元素.
NSLog(@"%@", [set1 anyObject]);
(结果:b).
(4). 是否包含.
if ([set1 containsObject:@"a"]) {
NSLog(@"包含!~");
} else {
NSLog(@"不包含!~");
}
(结果:包含!~).
3.可变集合.
NSMutableSet *set = [NSMutableSet set];
(1).添加元素.
[set addObject:@"R"];
[set addObject:@"T"];
[set addObject:@"Y"];
NSLog(@"%@", set);
(2).移除.
[set removeObject:@"R"];
NSLog(@"%@", set);
(3).NSCountedSet 计数集合.
NSCountedSet *countedSet = [NSCountedSet set]
[countedSet addObject:@"A"]
[countedSet addObject:@"A"]
[countedSet addObject:@"B"]
[countedSet addObject:@"C"]
NSLog(@"%@", countedSet)
(4).获取对象加入集合的次数.
NSLog(@"%lu", [countedSet countForObject:@"A"]);
(5).集合的遍历
for (NSString *temp in countedSet) {
NSLog(@"%@", temp);
}