如何更正 "Convenience initializer missing a 'self' call to another initializer" 的这些警告
How to correct these warnings for "Convenience initializer missing a 'self' call to another initializer"
我正在将我的代码从 iOS 8 更新到 iOS 9+,在 Xcode 8.2 中,我收到了这些警告并且不知道如何修复它。我在几年前看过一些 SO 帖子,建议解决方案是让编译器静音而不是调整代码。
在不影响 App Store 接受度的情况下处理这些警告的最佳方法是什么?
需要注意的一点是,它不在屏幕截图中,而是在同一个文件中 - 这个初始化程序:
-(instancetype)initWithCoder:(NSCoder*)decoder{
if(self=[super init]){
// If parent class also adopts NSCoding, replace [super init]
// with [super initWithCoder:decoder] to properly initialize.
text=[decoder decodeObjectForKey:@"text"];
fontname=[decoder decodeObjectForKey:@"font"];
}
return self;
}
这是随附的 Message.h:
#import <Foundation/Foundation.h>
@interface Message : NSObject <NSCoding>{
NSString *text;
NSString *fontname;
UIColor *color;
NSInteger speed;
NSInteger fontsize;
BOOL isMirror;
BOOL isReverse;
NSInteger transStyle;
NSInteger transDir;
NSInteger transSpeed;
BOOL isTransition;
BOOL isTextOnly;
NSInteger transOther;
NSInteger transPause;
}
@property (nonatomic,retain)NSString *text;
@property (nonatomic,retain)NSString *fontname;
@property (nonatomic,retain)UIColor *color;
@property (nonatomic,assign)NSInteger speed;
@property (nonatomic,assign)NSInteger fontsize;
@property (nonatomic,assign)BOOL isMirror;
@property (nonatomic,assign)BOOL isReverse;
@property (nonatomic,assign)NSInteger transStyle;
@property (nonatomic,assign)NSInteger transDir;
@property (nonatomic,assign)NSInteger transSpeed;
@property (nonatomic,assign)NSInteger transOther;
@property (nonatomic,assign)NSInteger transPause;
@property (nonatomic,assign)BOOL isTransition;
@property (nonatomic,assign)BOOL isTextOnly;
- (void) encodeWithCoder:(NSCoder*)encoder;
- (instancetype) initWithCoder:(NSCoder*)decoder NS_DESIGNATED_INITIALIZER;
@end
您有两个选择:
- 从
-initWithCoder:
中删除 NS_DESIGNATED_INITIALIZER
- 将
NS_DESIGNATED_INITIALIZER
添加到 -init
- 将
-init
改为调用[self initWithCoder:]
前两个最有道理;如果没有编码器,2 会相当困难。
苹果论坛的解释:
https://forums.developer.apple.com/thread/6757
The rules for designated initialisers are complex and I'm going to
bounce you to the docs for the general case. Curiously, I've found the
best explanation of this to be the "Initialization" section of The
Swift Programming Language, because the same concepts apply to both
Swift and Objective-C. In your specific case you should override -init
and have it fail at runtime. You should also tag it in your header
with NS_UNAVAILABLE which will allow the compiler to catch this in the
typical case. The above applies because your class can't possibly
operate without a Model, and thus you can't reasonably implement -init
in any useful way. If you could, you should. For example, if you were
creating your own string object, it would make sense for it to
implement -init by calling super and then initialising the string to
the empty string.
将以下行添加到您的 Message.h 文件
-(instancetype)init NS_DESIGNATED_INITIALIZER;
有关详细信息,请查看下面的 link
Using NS_DESIGNATED_INITIALIZER
我正在将我的代码从 iOS 8 更新到 iOS 9+,在 Xcode 8.2 中,我收到了这些警告并且不知道如何修复它。我在几年前看过一些 SO 帖子,建议解决方案是让编译器静音而不是调整代码。
在不影响 App Store 接受度的情况下处理这些警告的最佳方法是什么?
需要注意的一点是,它不在屏幕截图中,而是在同一个文件中 - 这个初始化程序:
-(instancetype)initWithCoder:(NSCoder*)decoder{
if(self=[super init]){
// If parent class also adopts NSCoding, replace [super init]
// with [super initWithCoder:decoder] to properly initialize.
text=[decoder decodeObjectForKey:@"text"];
fontname=[decoder decodeObjectForKey:@"font"];
}
return self;
}
这是随附的 Message.h:
#import <Foundation/Foundation.h>
@interface Message : NSObject <NSCoding>{
NSString *text;
NSString *fontname;
UIColor *color;
NSInteger speed;
NSInteger fontsize;
BOOL isMirror;
BOOL isReverse;
NSInteger transStyle;
NSInteger transDir;
NSInteger transSpeed;
BOOL isTransition;
BOOL isTextOnly;
NSInteger transOther;
NSInteger transPause;
}
@property (nonatomic,retain)NSString *text;
@property (nonatomic,retain)NSString *fontname;
@property (nonatomic,retain)UIColor *color;
@property (nonatomic,assign)NSInteger speed;
@property (nonatomic,assign)NSInteger fontsize;
@property (nonatomic,assign)BOOL isMirror;
@property (nonatomic,assign)BOOL isReverse;
@property (nonatomic,assign)NSInteger transStyle;
@property (nonatomic,assign)NSInteger transDir;
@property (nonatomic,assign)NSInteger transSpeed;
@property (nonatomic,assign)NSInteger transOther;
@property (nonatomic,assign)NSInteger transPause;
@property (nonatomic,assign)BOOL isTransition;
@property (nonatomic,assign)BOOL isTextOnly;
- (void) encodeWithCoder:(NSCoder*)encoder;
- (instancetype) initWithCoder:(NSCoder*)decoder NS_DESIGNATED_INITIALIZER;
@end
您有两个选择:
- 从
-initWithCoder:
中删除 - 将
NS_DESIGNATED_INITIALIZER
添加到-init
- 将
-init
改为调用[self initWithCoder:]
NS_DESIGNATED_INITIALIZER
前两个最有道理;如果没有编码器,2 会相当困难。
苹果论坛的解释: https://forums.developer.apple.com/thread/6757
The rules for designated initialisers are complex and I'm going to bounce you to the docs for the general case. Curiously, I've found the best explanation of this to be the "Initialization" section of The Swift Programming Language, because the same concepts apply to both Swift and Objective-C. In your specific case you should override -init and have it fail at runtime. You should also tag it in your header with NS_UNAVAILABLE which will allow the compiler to catch this in the typical case. The above applies because your class can't possibly operate without a Model, and thus you can't reasonably implement -init in any useful way. If you could, you should. For example, if you were creating your own string object, it would make sense for it to implement -init by calling super and then initialising the string to the empty string.
将以下行添加到您的 Message.h 文件
-(instancetype)init NS_DESIGNATED_INITIALIZER;
有关详细信息,请查看下面的 link
Using NS_DESIGNATED_INITIALIZER