2012-07-18 31 views
1

我试图在我的应用程序中跨两个进程销售一个对象。但是,当我测试我的代码时,接收出售对象的过程只是阻塞。我或多或少地遵循了http://www.mikeash.com/pyblog/friday-qa-2009-02-20-the-good-and-bad-of-distributed-objects.html上的示例代码。Cocoa中的分布式对象

下面是我的两个过程的代码:

/* 
* Description: Vends an object that the receiver can then access 
*    through the distributed object. 
*/ 

#import <Cocoa/Cocoa.h> 
#import <iostream> 

using namespace std; 

int main() { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    cout << "Starting vendor " << endl; 

    NSMutableArray *mutable_array; 
    [mutable_array addObject:@"Louis Lang"]; 
    [mutable_array addObject:@"John Doe"]; 

    NSConnection *connection = [NSConnection connectionWithReceivePort:[NSPort port] sendPort:nil]; 
    [connection setRootObject:mutable_array]; 
    [connection registerName:@"com.example.whatever"]; 

    [[NSRunLoop currentRunLoop] run]; 


    [pool drain]; 

    return 0; 
} 

而“接收器”

/* 
* Description: Receives the vended object from the server 
* 
*/ 

#import <Cocoa/Cocoa.h> 
#import <iostream> 

using namespace std; 

int main() { 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

    cout << "Starting receiver." << endl; 

    id theObject = (id)[NSConnection rootProxyForConnectionWithRegisteredName:@"com.example.whatever" host:nil]; 

    int the_count = [theObject count]; 

    NSLog(@"There are %i items in mutable_array", the_count); 

    [pool drain]; 

    return 0; 
} 

回答

2

你的代码似乎确定了其中大部分,分布式对象至少部分。但:

NSMutableArray* mutable_array = [[NSMutableArray alloc] init]; 
[mutable_array addObject:@"Louis Lang"]; 
[mutable_array addObject:@"John Doe"]; 

一定会帮助,我想。

如果您正在使用的Xcode 4.4或以上版本,这也将这样做:

NSArray* array = @[ @"Louis Lang", @"John Doe" ]; 
+0

就是这样,我被贩卖nil对象。谢谢! – Julio 2012-07-18 19:52:44