我正在将一个Objective-C项目的一些属性暴露给Swift(基于this repo),但是我没有Objective-C的经验,所以在这里我有点力不从心,请您谅解。
我想知道如何正确释放一个nonnull属性(或者是否需要释放!)。我已经通过将其设置为null(与对可空partOfSpeech所做的操作相同)来暂时dealloc非null属性surface。然而,这会提示以下警告:
Null passed to a callee that requires a non-null argument
因此我想知道它是否多余。在Node类的dealloc块期间,有没有其他处理nonnull属性的方法?
给出了接口node.h:
...而且实现部分,
“...而且考虑到一个
我想知道如何正确释放一个nonnull属性(或者是否需要释放!)。我已经通过将其设置为null(与对可空partOfSpeech所做的操作相同)来暂时dealloc非null属性surface。然而,这会提示以下警告:
Null passed to a callee that requires a non-null argument
因此我想知道它是否多余。在Node类的dealloc块期间,有没有其他处理nonnull属性的方法?
给出了接口node.h:
@interface Node : NSObject {
NSString *surface;
NSString *partOfSpeech;
}
@property (nonatomic, retain, nonnull) NSString *surface;
@property (nonatomic, retain, nullable) NSString *partOfSpeech;
- (nullable NSString *)partOfSpeech;
@end
...而且实现部分,
node.m
:
@implementation Node
@synthesize surface;
@synthesize partOfSpeech;
// surface is assumed to be set post-initialisation.
- (void)setPartOfSpeech:(NSString *)value {
if (partOfSpeech) [partOfSpeech release];
partOfSpeech = value ? [value retain] : nil;
}
- (NSString *)partOfSpeech {
if (!features || [features count] < 1) return nil;
return [features objectAtIndex:0];
}
- (void)dealloc {
// WARNING: "Null passed to a callee that requires a non-null argument"
self.surface = nil;
self.partOfSpeech = nil;
[super dealloc];
}
@end
“...而且考虑到一个
Node
的生命周期是这样的:” Node *newNode = [Node new];
newNode.surface = [[[NSString alloc] initWithBytes:node->surface length:node->length encoding:NSUTF8StringEncoding] autorelease];
// ... Do stuff with newNode (eg. add to array of Node)...
[newNode release];