Objective-C:Foundation 프레임워크 - 일반 클래스 - NSMutable Dictionary

2961 단어
바로 코드에 들어가세요.
#import <Foundation/Foundation.h>

@interface Student : NSObject
@property (nonatomic, retain) NSString *name;

+ (id)studentWithName:(NSString *)name;
@end

#import "Student.h"

@implementation Student

+ (id)studentWithName:(NSString *)name {
    Student *stu = [[Student alloc] init];
    stu.name = name;
    return [stu autorelease];
}

- (void)dealloc {
    NSLog(@"%@    ", _name);
    //   name
    [_name release];
    [super dealloc];
}
@end

 
#import <Foundation/Foundation.h>
#import "Student.h"

#pragma mark        
void dictUse() {
    //         
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    Student *stu1 = [Student studentWithName:@"stu1"];
    Student *stu2= [Student studentWithName:@"stu2"];
    
    //     
    // stu1     +1
    [dict setObject:stu1 forKey:@"k1"];
    NSLog(@"stu1:%zi", [stu1 retainCount]);
    
    //       other     dict 
    NSDictionary *other = [NSDictionary dictionaryWithObject:@"v2" forKey:@"k2"];
    [dict addEntriesFromDictionary:other];
    
    //         
    // [dict removeAllObjects];
    
    //   k1     stu1,stu1    release  
    [dict removeObjectForKey:@"k1"];
    NSLog(@"stu1:%zi", [stu1 retainCount]);
    
    //     key   value
    // [dict removeObjectsForKeys:[NSArray arrayWithObject:@"k1"]];
    
    //       ,     key value     -1,    stu1 release  
}

좋은 웹페이지 즐겨찾기