我在类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没有 super class ,因此运行时抱怨它没有实现某些基本行为(正确地如此)。您可能打算继承NSObject:

@interface MyUtils : NSObject {
}

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

09-07 10:30
查看更多