我有以下课程:
---- .h文件
#import <Foundation/Foundation.h>
#import "MapperProtocoll.h"
@interface ServiceRequest : NSObject {
}
@property (strong, nonatomic) NSString *url;
@property (strong, nonatomic) NSString *postData;
@property (strong, nonatomic) NSNumber *requestId;
@property (strong, nonatomic) id<ServiceMapperDelegate> mapper;
-(id)initWithUrl:(NSString *)url andWithPostdata:(NSString *)postData;
@end
------- .m文件
#import "ServiceRequest.h"
@implementation ServiceRequest
@synthesize url = _url, postData = _postData, requestId = _requestId, mapper = _mapper;
-(id)initWithUrl:(NSString *)url andWithPostdata:(NSString *)postData {
if (self = [super init]) {
_url = url;
_postData = postData;
self.requestId = [NSNumber numberWithInt:-1]; // HERE IS THE PROBLEM
}
return self;
}
@end
为什么 self.requestId = [NSNumber numberWithInt:-1]; 工作但是 _requestId = [NSNumber numberWithInt:-1]; 抛出运行时错误?
答案 0 :(得分:3)
类方法[NSNumber numberWithInt:-1]
正在返回自动释放的值。使用合成的setter方法时,该值将由setter保留。当你绕过setter时,没有保留...所以一旦自动释放池被耗尽,你就会有一个悬空指针。