2

我从 xcode 创建了一个可可 dylib 模板,并根据苹果文档添加了头文件

Person.h 有以下代码

#import <Cocoa/Cocoa.h>

@protocol Person

- (void)setName:(NSString*)name;

- (NSString*)name;

@end

@interface Person : NSObject <Person>{

@private

    NSString* _person_name;

}

并且Titling.h 有以下代码

#import <Cocoa/Cocoa.h>

#import "Person.h"



@protocol Titling

- (void)setTitle:(NSString*)title;

@end

@interface Person (Titling) <Titling> {

}

@end

这就是我在 dylib.and 编译的所有代码,没有错误构建成功。

在此之后,我创建了一个新的可可应用程序,将 dylib(cocoaLibrary.dylib) 拖到资源文件夹可可应用程序中。在 applicatiodidFinishLaunching 中添加了代码

#import "loadCocoaLibAppDelegate.h"

#import <dlfcn.h>

#import "Person.h"

#import "Titling.h"

@implementation loadCocoaLibAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application 


    //NSAutoreleasePool* pool = [NSAutoreleasePool new];



    // Open the Person library.

    void* lib_handle = dlopen("./libPerson.dylib", RTLD_LOCAL);

    if (!lib_handle) {

        exit(EXIT_FAILURE);

    }



    // Get the Person class (required with runtime loaded libraries).

    Class Person_class = objc_getClass("Person");

    if (!Person_class) {

        exit(EXIT_FAILURE);

    }



    // Create an instance of Person.

    NSObject<Person,Titling>* person = [Person_class new];



    // Use person.

    [person setName:@"Perrine LeVan"];

    NSLog(@"[%s] main: [person name] = %@", __FILE__, [person name]);

    [person setTitle:@"Ms."];

    NSLog(@"[%s] main: [person name] = %@", __FILE__, [person name]);



    // Dispose of person.

    [person release];



    // Close the Person library.

    if (dlclose(lib_handle) != 0) {

        exit(EXIT_FAILURE);

    }



   // [pool release];

}

@end

Person.h:没有这样的文件或目录

Titling.h:没有这样的文件或目录

真的我不知道如何解决这个问题。请帮忙。

4

0 回答 0