gpt4 book ai didi

objective-c - Cocoa 中的分布式对象

转载 作者:行者123 更新时间:2023-12-03 17:04:58 25 4
gpt4 key购买 nike

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

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

/*
* 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;
}

最佳答案

您的代码对于大部分来说似乎都不错,至少是分布式对象部分。但是:

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" ];

关于objective-c - Cocoa 中的分布式对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11548458/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com