开发者

Accessing mutable array value

I have created the one mutable array in appdelegate. I am trying to retrieve values of that mutable array in another app. But it is crashing at that point. Below is the code I have declared in appdelegate:

savedLocation = [[NSMutableArray alloc] init];
 savedLocation = [[NSMutableArray arrayWithObjects:
       [NSNumber numberWithInteger:0],
       nil] retain];

Below is code in which I am trying to access the array values in another application through appdelegate:

App开发者_JAVA技巧Delegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
 NSInteger selection = [[appDelegate.savedLocation objectAtIndex:0] integerValue];

But it is crashing at:

NSInteger selection = [[appDelegate.savedLocation objectAtIndex:0] integerValue];

Please tell me the reason why it is crashing and the correct way of doing this also.


If you really have 2 different Objective-C applications running, keep in mind that each one will have its own [UIApplication sharedApplication]. If I understand your question correctly, you are assuming that you will be able to pass information between applications using this mechanism and that is simply not the case. Each app will have its own sharedApplication and corresponding app delegate, and will not be able to see the members of the other.

Since you are using UIApplication (rather than NSApplication), I am inferring that you are using Cocoa Touch (for iPhone and iPod Touch), and so you are probably interested in the mechanisms that are available for sharing data between applications. There are a number of possible approaches, including a custom URL handler (to allow one app to launch the other app with some particular parameters), or using a network-dependent sync mechanism where you store data from both applications in some shared server location on the Internet. You should keep in mind, however, that:

  1. Only one of your applications will ever be running on the Cocoa Touch device at any one time. So, the in-memory member variables of the app delegate will be released as soon as the app that instantiated them exits (to make way for the other app).

  2. There is no way I know of for one Cocoa Touch application to read data saved by another Cocoa Touch application onto the device. The operating system strictly sandboxes all individual applications running on the device so one cannot read data saved by another.

This question is similar to what I think you're asking.


If you are using Objective-C and Cocoa for a Mac OS X application you can find many system to pass information between two running applications i.e. using Notifications, the File System, Distant Objects, the PasteBoard some useful information can be found here: http://developer.apple.com/mac/library/navigation/index.html#section=Topics&topic=Interapplication%20Communication

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜