objective-c - 应用程序在上课时崩溃并且不会赶上
问题描述
在我的 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 中记录异常?另外,我对目标 C 非常陌生(我被要求坚持使用目标 c),如果有人对代码有任何建议,请告诉我。
解决方案
您正在为引用/指针类型使用分配: @property 在 Objective-C 中保留、分配、复制、非原子
它们可能应该被声明为副本,因为我认为这是一种价值对象。
因为没有抛出异常,所以没有捕获到异常。控制流的抛出/捕获异常在 Objective-C 中并不常见
您不需要为 @properties 编写显式设置器函数
您应该更喜欢使用 BOOL 类型而不是 Boolean,其值为 YES/NO 而不是 true/false。
您应该从 init 返回 instancetype 而不是 id,至少在相当现代的 Objective C 中
考虑制作一个包含所有属性的初始化程序 (initWithRoomName:clientID:) 并使它们只读一次
您不需要在标头中声明 -(id) init ,因为它是从 NSObject 获取的
推荐阅读
- docker-compose - Docker-compose.yml Volummes: 和 -mount 命令结果不一样
- linux - 在 LINUX 服务器上使用 torch7 运行 LUA
- c# - 从字典实例化泛型类型
- html - html中输入标签的属性“placeholder”和“aria-label”
- android - Firebase onNewToken 没有刷新我的令牌
- c# - 如何在不使用异常的情况下判断缓冲区是 Google 协议缓冲区(Protobuf)消息还是其他内容?
- java - Java 字符串的哈希码是在创建字符串时创建的,还是仅在第一次调用哈希码时创建?
- c++ - C++ 中的 Mariadb Kerberos 身份验证
- javascript - 反应:未安装组件中的 setState
- c++ - 如何避免这种代码重复?