BKObject是自定义对象,我想将多个BKObject放入数组中。
BKViewController:
#import <UIKit/UIKit.h>
#import "BKObject.h"
@interface BKViewController : UIViewController
@property (strong, nonatomic) NSArray *data;
@property (weak, nonatomic) BKObject *tmpObject;
@end
BKViewController.m:
#import "BKViewController.h"
@implementation BKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *arr = [[NSMutableArray alloc] init];
for(NSInteger i = 0; i < 100000; i++){
[arr addObject:[[BKObject alloc] initWithName:@""]];
}
self.data = [NSArray arrayWithArray:arr];
__weak BKObject *weakMutableObject = arr[0];
[arr removeAllObjects];
NSLog(@"%@", weakMutableObject); // print out the object, why?
__weak BKObject *weakObject = self.data[0];
self.data = nil;
NSLog(@"%@", weakObject); // print out the object again, but why?
self.tmpObject = [[BKObject alloc] initWithName:@""];
NSLog(@"%@", self.tmpObject); // print null, very clear
}
@end
我很好奇为什么前两个NSLog消息显示一个对象而不是null(如最后一个NSLog中所示)。我正在使用最新的Xcode 5.0.1和iOS 7 SDK。