Objective-C and Cocoa : crash when calling a class function without entering the function
-
14-10-2019 - |
Pergunta
I have a class function (declared and implemented) in a class MyUtils. When I call this function my app crashes. In the debugger I have a breakpoint on the first action of the "theFunction" function. And this breakpoint is never reached.
Here is the code :
// =================================================================================================
// 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**
The console says :
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
If I replace the call by NSString *item = @"youyou";
then everything is ok.
Forcing a retain on aPreviousNSString before the call does not change anything. Do you have an idea of what is happening ?
Solução
You declared MyUtils
without a superclass, so the runtime is complaining that it doesn't implement certain basic behaviors (rightfully so). You probably meant to inherit from NSObject
:
@interface MyUtils : NSObject {
}
+ (NSString*) changeDateFormat_fromFormat:(NSString*)sourceFormat sourceDateString:(NSString*)sourceDateString destFormat:(NSString*)destFormat;
+ (void) simpleAlert_ok:(NSString*)alertTitle message:(NSString*)alertMessage;
@end
Outras dicas
You do not have a superclass declared on your MyUtils class. To fix it, just change @interface MyUtils
to @interface MyUtils : NSObject
. If you do not declare a superclass, you have to provide all of the required methods yourself.
Your class needs to be of some object type for it to be compilable. The base object in Objective-C for iOS is NSObject, all classes inherit from it.
You want to change the line that says:
@interface MyUtils
to this:
@interface MyUtils : NSObject {
}
+ (NSString *) ... ... ...
For more information on NSObject, see the NSObject Class reference in the Apple Developer Library.