【问题标题】:Objective-C and Cocoa : crash when calling a class function without entering the functionObjective-C和Cocoa:调用类函数而不进入函数时崩溃
【发布时间】:2011-01-02 23:46:58
【问题描述】:

我在 MyUtils 类中有一个类函数(声明和实现)。 当我调用此函数时,我的应用程序崩溃了。在调试器中,我在“theFunction”函数的第一个动作上有一个断点。而且这个断点永远不会到达。

代码如下:

// =================================================================================================
// MyUtils.m
// =================================================================================================
+ (NSString*) changeDateFormat_fromFormat:(NSString*)sourceFormat sourceDateString:(NSString*)sourceDateString destFormat:(NSString*)destFormat {
    if (sourceDateString == nil) return (nil); **<-- breakpoint here**

    NSDate* aDate = [NSDate dateFromString:sourceFormat theDateString:sourceDateString];
    return ([aDate stringValueWithFormat:destFormat]);
}

// ===================================================================
// MyUtils.h
// ===================================================================
@interface MyUtils
+ (NSString*) changeDateFormat_fromFormat:(NSString*)sourceFormat sourceDateString:(NSString*)sourceDateString destFormat:(NSString*)destFormat;
+ (void) simpleAlert_ok:(NSString*)alertTitle message:(NSString*)alertMessage;

@end


// ===================================================================
// Elsewhere.m
// ===================================================================
- (void) aFunction:(SomeClass*)someParam {
    SomeOtherClass* val = nil;
    NSString* intitule = nil;


    intitule = [MyUtils changeDateFormat_fromFormat:@"yyyyMMdd" sourceDateString:@"toto" destFormat:@"EEEE dd MMMM yyyy"]; **<-- crash here**

控制台说:

2011-01-03 02:05:07.188 Learning Project[1667:207] *** NSInvocation: warning: object 0xe340 of class 'MyUtils' does not implement methodSignatureForSelector: -- trouble ahead
2011-01-03 02:05:07.188 Learning Project[1667:207] *** NSInvocation: warning: object 0xe340 of class 'MyUtils' does not implement doesNotRecognizeSelector: -- abort

如果我用NSString *item = @"youyou"; 替换呼叫,那么一切正常。

在调用之前强制保留 aPreviousNSString 不会改变任何内容。 你知道发生了什么吗?

【问题讨论】:

  • 我们能看到更多代码吗?我猜MyUtils 是一个单身人士。对吗?
  • 这似乎不是您的实际代码。在创建模拟示例时,您可能错过了崩溃的真实代码的关键方面。请告诉我们您实际使用的是什么。
  • @Moshe 不,那就是[[MyUtils sharedUtils] ...]
  • @JacobRelkin - 没错,但这个概念听起来像是一个单例实用程序类。另外,请记住,命名约定不是必需的 - 可以是他想要的任何名称。
  • 控制台输出是什么?什么是堆栈跟踪?

标签: objective-c crash debugging function-call


【解决方案1】:

您在没有超类的情况下声明了MyUtils,因此运行时抱怨它没有实现某些基本行为(理所当然)。你可能打算从NSObject继承:

@interface MyUtils : NSObject {
}

+ (NSString*) changeDateFormat_fromFormat:(NSString*)sourceFormat sourceDateString:(NSString*)sourceDateString destFormat:(NSString*)destFormat;
+ (void) simpleAlert_ok:(NSString*)alertTitle message:(NSString*)alertMessage;
@end

【讨论】:

  • 就是这样......虽然这是错误的原因,但我没有。任何类都必须至少继承 NSObject...这是我脑子里写的 :-) 但是...有人能解释一下为什么如果我在调用函数之前等待调试器,有时它在没有超类的情况下也能正常工作吗?
【解决方案2】:

您的 MyUtils 类上没有声明超类。要修复它,只需将@interface MyUtils 更改为@interface MyUtils : NSObject。如果不声明超类,则必须自己提供所有必需的方法。

【讨论】:

  • 好的,但是为什么如果我在调用函数之前等待调试器,有时它可以在没有超类的情况下正常工作?
  • 这很有趣。我猜当你等待时,运行时直接查看类而不是调用 methodSignatureForSelector: 和 doesNotRespondToSelector:,但我不知道它为什么会这样做。
【解决方案3】:

你的类需要是某种对象类型才能被编译。 iOS 的 Objective-C 中的基础对象是 NSObject,所有的类都继承自它。

您想要更改以下内容:

@interface MyUtils

到这里:

@interface MyUtils : NSObject { 


}

  + (NSString *) ... ... ...

有关 NSObject 的更多信息,请参阅NSObject Class reference in the Apple Developer Library

【讨论】:

  • 好的,但是为什么如果我在调用函数之前等待调试器,有时它可以在没有超类的情况下正常工作?
  • 我建议清理你的项目(Command+Shift+K)然后重建它。看看会发生什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多