2011-01-02 2 views
1

클래스 MyUtils에서 클래스 함수 (선언되고 구현 된)가 있습니다. 이 함수를 호출하면 앱이 다운됩니다. 디버거에서 "theFunction"함수의 첫 번째 동작에 중단 점이 있습니다. 그리고이 중단 점은 절대로 도달하지 않습니다. NSString *item = @"youyou"; 다음 모든 것이 괜찮아으로 내가 전화를 교체 할 경우Objective-C 및 Cocoa : 함수를 입력하지 않고 클래스 함수를 호출 할 때 충돌이 발생합니다.

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 

:

// ================================================================================================= 
// 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** 

콘솔은 말한다 : 여기

는 코드입니다.

호출이 변경되기 전에 이전 NSString에 대한 보존을 강제 수행하면 아무 것도 변경되지 않습니다. 무슨 일이 일어나고 있는지 알고 계신가요?

+1

더 많은 코드를 볼 수 있습니까? 나는 'MyUtils'가 싱글 톤이라고 추측한다. 그 맞습니까? – Moshe

+2

이것은 실제 코드가 아닙니다. 모의 예제를 만들 때 충돌하는 실제 코드의 중요한 부분을 놓친 것일 수 있습니다. 실제로 사용하고있는 것을 보여주십시오. –

+0

@Moshe 아니, 그렇다면'[[MyUtils sharedUtils] ...]' –

답변

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 
+0

그게 전부입니다 ... 나는 이것이 오류의 원인 이었지만 그렇게하지 않았습니다. 모든 클래스는 적어도 NSObject를 상속해야합니다 ... 그건 내 머리 속에 쓰여졌습니다 :-)하지만 ... 누군가가 함수를 호출하기 전에 디버거를 기다리는 이유를 설명 할 수 있습니까, 때로는 수퍼 클래스없이 잘 작동합니까? – Oliver

3

당신은 슈퍼 클래스가 MyUtils 클래스에 선언하지 않습니다. 문제를 해결하려면 @interface MyUtils@interface MyUtils : NSObject으로 변경하십시오. 수퍼 클래스를 선언하지 않으면 필요한 모든 메소드를 직접 제공해야합니다.

+0

ok,하지만 함수를 호출하기 전에 디버거를 기다리는 경우 가끔 슈퍼 클래스없이 제대로 작동합니까? – Oliver

+1

재미 있습니다. 나는 당신이 기다릴 때 런타임이 직접 클래스를 통해 methodSignatureForSelector :와 doesNotRespondToSelector :를 호출하는 대신에 보았을 것이다. 그러나 나는 왜 그렇게 할 것인지 전혀 모른다. – ughoavgfhw

1

클래스가 컴파일 가능한 개체 유형이어야합니다. iOS 용 Objective-C의 기본 객체는 NSObject이며 모든 클래스는 그 객체로부터 상속받습니다. NSObject Class reference in the Apple Developer Library를 참조 NSObject의에 대한 자세한 내용은

@interface MyUtils : NSObject { 


} 

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

이에

@interface MyUtils

:

당신은 말한다 선을 변경하고 싶습니다.

+0

ok,하지만 함수를 호출하기 전에 디버거를 기다리는 경우 가끔 슈퍼 클래스없이 제대로 작동합니까? – Oliver

+0

프로젝트 정리 (Command + Shift + K)를 한 다음 다시 빌드하는 것이 좋습니다. 무슨 일이 일어나는지보십시오. – Moshe

관련 문제