1

我在 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 不会改变任何内容。你知道发生了什么吗?

4

3 回答 3

3

您声明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
于 2011-01-03T00:17:06.223 回答
3

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

于 2011-01-03T00:17:31.917 回答
1

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

您想更改显示以下内容的行:

@interface MyUtils

对此:

@interface MyUtils : NSObject { 


}

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

有关 NSObject 的更多信息,请参阅Apple Developer Library 中的 NSObject 类参考

于 2011-01-03T00:24:29.837 回答