首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Cocoa中的分布式对象

Cocoa中的分布式对象
EN

Stack Overflow用户
提问于 2012-07-19 03:00:03
回答 1查看 1.8K关注 0票数 1

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

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

代码语言:javascript
复制
/*
 * 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;
}

和“接收者”

代码语言:javascript
复制
/*
 * 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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-19 03:47:24

你的代码看起来很不错,至少是分布式对象的一部分。但是:

代码语言:javascript
复制
NSMutableArray* mutable_array = [[NSMutableArray alloc] init];
[mutable_array addObject:@"Louis Lang"];
[mutable_array addObject:@"John Doe"];

我想肯定会有帮助的。

如果你使用的是Xcode 4.4+,下面的代码也可以:

代码语言:javascript
复制
NSArray* array = @[ @"Louis Lang", @"John Doe" ];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11548458

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档