首页 > 解决方案 > 应用程序在上课时崩溃并且不会赶上

问题描述

在我的 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),如果有人对代码有任何建议,请告诉我。

错误 在此处输入图像描述

标签: objective-cxcode

解决方案


您正在为引用/指针类型使用分配: @property 在 Objective-C 中保留、分配、复制、非原子

它们可能应该被声明为副本,因为我认为这是一种价值对象。

因为没有抛出异常,所以没有捕获到异常。控制流的抛出/捕获异常在 Objective-C 中并不常见

您不需要为 @properties 编写显式设置器函数

您应该更喜欢使用 BOOL 类型而不是 Boolean,其值为 YES/NO 而不是 true/false。

您应该从 init 返回 instancetype 而不是 id,至少在相当现代的 Objective C 中

考虑制作一个包含所有属性的初始化程序 (initWithRoomName:clientID:) 并使它们只读一次

您不需要在标头中声明 -(id) init ,因为它是从 NSObject 获取的


推荐阅读