2016-09-07 4 views
1

'개인'에 액세스하는 데 많은 질문이 있습니다 (기술적으로 Obj-C의 개인 메소드와 같은 것은 없습니다). Obj-C. 그리고 SomeClass에 대한 보이지 않는 @interface를 다루는 많은 질문들이있다. 선택자 'SomeMethod'가 선언되어있다. 그러나 둘 모두를 다루는 사람은 없습니다.Objective 다른 .mm 파일에서 하나의 .mm 파일로 정의 된 'private'메소드를 실행했습니다.

그래서 여기에 몇 가지 코드가 있습니다. Example.h

#import <Cocoa/Cocoa.h> 

@interface Example : NSView 

@end 

Example.mm

#import "Example.h" 
@interface Example() 
- (void) printWordOne:(NSString*) firstWorld wordTwo:(NSString*) secondWord; 
@end 


@implementation Example 

- (void)drawRect:(NSRect)dirtyRect { 
    [super drawRect:dirtyRect]; 

    // Drawing code here. 
} 

- (void) printWordOne:(NSString*) firstWorld wordTwo:(NSString*) secondWord{ 
    NSLog(@"The two words are %@ %@", firstWorld, secondWord); 
} 

@end 

ViewController.h는

#import <Cocoa/Cocoa.h> 
#import "Example.h" 
@interface ViewController : NSViewController{ 
    IBOutlet Example *example; 

} 

@end 

함께 IBOutlet 스토리 보드로 연결되었다.

ViewController.mm

#import "ViewController.h" 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    // Do any additional setup after loading the view. 
    [example printWordOne:@"Hello" wordTwo: @"World"]; 
} 

- (void)setRepresentedObject:(id)representedObject { 
    [super setRepresentedObject:representedObject]; 

    // Update the view, if already loaded. 
} 

@end 

오전 데 문제는이 메소드 호출이다. [example printWordOne:@"Hello" wordTwo: @"World"];

오류가 No visible @interface for 'Example' declares the selector 'printWordOne:wordTwo'

내가 Example.h 파일에 선언하지 않고 그 함수를 호출 할 수있는 방법이 필요하다. 나는이 방법 목록을 얻을 ViewController.mm에서 그 방법을 나열 할 수 있습니다 class_copyMethodList를 사용하여 알고

duplicate symbol _OBJC_CLASS_$_Example in: 
    /path/Example.o 
    /path/ViewController.o 
duplicate symbol _OBJC_METACLASS_$_Example in: 
    /path/Example.o 
    /path/ViewController.o 
ld: 2 duplicate symbols for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 

: 내 ViewController.mm 파일 I #import Example.mm 경우 나는 다음과 같은 얻을. 그러나이 방법을 실행하기 위해 어쨌든 다시 거기에 있습니다.

도움을 주시면 감사하겠습니다.

답변

1

당신은 단순히 당신의 ViewController.mm 내부에 private 메소드 선언과 Example 클래스에 카테고리를 선언 할 수

#import "ViewController.h" 

@interface Example() 
- (void) printWordOne:(NSString*) firstWorld wordTwo:(NSString*) secondWord; 
@end 

@implementation ViewController 
// ... 
@end 
+0

감사 보리스, 나는 간단하게 뭔가를 잃어버린 것을 알고 있었다. – user2517182