应用程序在使用 class 时崩溃并且无法捕获
App crashing while working with class and not going to catch
在我的 AppDelegate.m 中,我正在做这样的事情
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
@try {
// initalizing Meeting config
MeetingConfig *config = [[MeetingConfig alloc] init];
NSLog(@"Initalized Meeting Config: %@", config);
[config setRoomName:@"test123"];
NSLog(@"SetRoom name for Meeting config: %@", config.roomName);
NSString *clientId = @"";
NSLog(@"Unused Client id is: %@", clientId);
//Call UIView from here
}@catch (NSException *exception) {
NSLog(@"exception: %@", exception);
}
return YES;
}
我的 MeetingConfig.m 文件看起来像这样
@implementation MeetingConfig
- (id) init
{
if (self = [super init]) {
self.apiBase = @"https://api.in";
self.showSetupScreen = false;
self.autoTune = true;
}
return self;
}
- (void) setAuthToken:(NSString *)authToken
{
self.authToken = authToken;
}
- (void) setApiBase:(NSString *)apiBase
{
self.apiBase = apiBase;
}
// more code
MeetingConfig 看起来像这样
#import <Foundation/Foundation.h>
@interface MeetingConfig : NSObject
@property (nonatomic, assign) NSString* roomName;
@property (nonatomic, assign) NSString* authToken;
@property (nonatomic, assign)Boolean autoTune;
@property (nonatomic, assign)NSString* apiBase;
@property (nonatomic, assign)Boolean showSetupScreen;
- (void) setRoomName:(NSString *)roomName;
- (void) setAuthToken:(NSString *)authToken;
- (void) setShowSetupScreen:(Boolean)showSetupScreen;
- (void) setAutoTuneEnabled:(Boolean)autoTune;
- (id) init;
@end
有人可以帮我确定我在这里做错了什么吗?为什么它不在 NSLog 中记录异常?另外,我是 objective C 的超级新手(我被要求坚持使用 Objective c),如果有人对代码有任何建议,请告诉我。
错误
您正在为 reference/pointer 类型使用分配:@property retain, assign, copy, nonatomic in Objective-C
它们可能应该声明为副本,因为我认为这是一种价值 object。
没有捕获到异常,因为没有抛出异常。 Throwing/catching 控制流异常在 Objective-C
中并不常见
您不需要为@properties
编写明确的setter 函数
您应该更喜欢使用 BOOL 类型而不是布尔值,值是 YES/NO 而不是 true/false。
你应该 return instancetype 而不是来自 init 的 id,至少在合理的现代 Objective C
考虑制作一个采用所有属性 (initWithRoomName:clientID:) 的初始化程序,并让它们在设置后只读
你不需要在你的 header 中声明 -(id) init 因为它是从 NSObject
在我的 AppDelegate.m 中,我正在做这样的事情
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
@try {
// initalizing Meeting config
MeetingConfig *config = [[MeetingConfig alloc] init];
NSLog(@"Initalized Meeting Config: %@", config);
[config setRoomName:@"test123"];
NSLog(@"SetRoom name for Meeting config: %@", config.roomName);
NSString *clientId = @"";
NSLog(@"Unused Client id is: %@", clientId);
//Call UIView from here
}@catch (NSException *exception) {
NSLog(@"exception: %@", exception);
}
return YES;
}
我的 MeetingConfig.m 文件看起来像这样
@implementation MeetingConfig
- (id) init
{
if (self = [super init]) {
self.apiBase = @"https://api.in";
self.showSetupScreen = false;
self.autoTune = true;
}
return self;
}
- (void) setAuthToken:(NSString *)authToken
{
self.authToken = authToken;
}
- (void) setApiBase:(NSString *)apiBase
{
self.apiBase = apiBase;
}
// more code
MeetingConfig 看起来像这样
#import <Foundation/Foundation.h>
@interface MeetingConfig : NSObject
@property (nonatomic, assign) NSString* roomName;
@property (nonatomic, assign) NSString* authToken;
@property (nonatomic, assign)Boolean autoTune;
@property (nonatomic, assign)NSString* apiBase;
@property (nonatomic, assign)Boolean showSetupScreen;
- (void) setRoomName:(NSString *)roomName;
- (void) setAuthToken:(NSString *)authToken;
- (void) setShowSetupScreen:(Boolean)showSetupScreen;
- (void) setAutoTuneEnabled:(Boolean)autoTune;
- (id) init;
@end
有人可以帮我确定我在这里做错了什么吗?为什么它不在 NSLog 中记录异常?另外,我是 objective C 的超级新手(我被要求坚持使用 Objective c),如果有人对代码有任何建议,请告诉我。
错误
您正在为 reference/pointer 类型使用分配:@property retain, assign, copy, nonatomic in Objective-C
它们可能应该声明为副本,因为我认为这是一种价值 object。
没有捕获到异常,因为没有抛出异常。 Throwing/catching 控制流异常在 Objective-C
中并不常见您不需要为@properties
编写明确的setter 函数您应该更喜欢使用 BOOL 类型而不是布尔值,值是 YES/NO 而不是 true/false。
你应该 return instancetype 而不是来自 init 的 id,至少在合理的现代 Objective C
考虑制作一个采用所有属性 (initWithRoomName:clientID:) 的初始化程序,并让它们在设置后只读
你不需要在你的 header 中声明 -(id) init 因为它是从 NSObject