objective-c - 使用 NSObjects 将文件保存到文档 NSMutableArray 的问题
<p><p>我是一名新手 iOS 开发人员。
我编写了一个小应用程序,它保存了一个 <code>NSMutableArray</code> 数组,其中包含从 <code>NSObject</code> 派生的对象。
应用程序进行保存,但文件未在文档目录中创建,应用程序无法读取。
这个问题在模拟器和我的 iPhone 3gs 4.2.1 上都有</p>
<p>我在 <code>appDelegate</code> 类中的 <code>NSMutableArray</code> 定义:</p>
<pre><code>@property (nonatomic,retain, readwrite) NSMutableArray *places;
</code></pre>
<p>我的 NSObject 类:</p>
<pre><code> #import <Foundation/Foundation.h>
@interface Place : NSObject {
NSString *name;
NSString *location;
}
-(id) init:(NSString *)name: (NSString *)location;
@property (retain,nonatomic,readwrite) NSString *name;
@property (retain,nonatomic,readwrite) NSString *location;
@end
</code></pre>
<p>我的 StorageService 库类:</p>
<pre><code> #import "StorageService.h"
@implementation StorageService
-(id) init {
self = ;
if (self != nil) {
}
return self;
}
-(void) saveArrayToFile:(NSString*) filename : (NSMutableArray *)arrayToSave{
// get full path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fullPath = ;
fullPath = ;
NSLog(@"Save in %@",fullPath);
;
}
-(NSMutableArray*) readArrayFromFile:(NSString *)filename {
// get full path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fullPath = ;
fullPath = ;
if ([ fileExistsAtPath:fullPath]) {
NSMutableArray *data = [ initWithContentsOfFile:fullPath];
if (data == nil) {
data = [ init];
}
NSLog(@"Read from %@",fullPath);
return data;
} else {
NSMutableArray *data = [ initWithContentsOfFile:fullPath];
return data;
}
}
-(void) dealloc {
;
}
@end
</code></pre>
<p>和<code>appDelegate</code>中的My函数:</p>
<pre><code> -(void) saveApplicationData {
;
}
-(void) loadApplicationData {
self.places = ;
}
</code></pre>
<p>这是我的类,它对文件名保持不变:</p>
<pre><code> #import <Foundation/Foundation.h>
extern NSString * const PLACES_FILE = @"Places.dat";
@interface ApplicationConstants : NSObject {
}
@end
</code></pre>
<p>那么有什么问题呢?</p>
<p>谢谢你们。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您想要的是让 <code>Place</code> 符合 <code>NSCoding</code> 协议(protocol),以允许对文件进行序列化<em>(如果需要,还可以在内存中数据)</em></p>
<p>将 <code>Place</code> 扩展为 <em>(我还更改了 init 方法的名称,因为您的名字违反了 iOS 的所有命名惯例)</em>:</p>
<pre><code>@interface Place : NSObject <NSCoding> {
NSString *name;
NSString *location;
}
-(id)initWithName:(NSString *)name location:(NSString *)location;
@property (retain,nonatomic,readwrite) NSString *name;
@property (retain,nonatomic,readwrite) NSString *location;
@end
</code></pre>
<p>您的实现非常简单,但您还需要实现 <code>NSCoding</code> 协议(protocol)定义的两个方法:</p>
<pre><code>@implementation Place
@synthesize name, location;
-(id)initWithName:(NSString *)aName location:(NSString *)aLocation {
self = ;
if (self) {
self.name = aName;
self.location = aLocation;
}
return self;
}
-(id)initWithWithCoder:(NSCoder)decoder {
self = ;
if (self) {
self.name = ;
self.location = [decoder decodeObjectForKey:@"location";
}
return self;
}
-(void)encodeWithCoder:(NSCoder*)encoder {
;
;
;
}
@end
</code></pre>
<p>有了这个,将 <code>places</code> 数组保存到磁盘就像这样简单:</p>
<pre><code>;
</code></pre>
<p>解码同样简单:</p>
<pre><code>places = [ retain];
</code></pre></p>
<p style="font-size: 20px;">关于objective-c - 使用 NSObjects 将文件保存到文档 NSMutableArray 的问题,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/7038428/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/7038428/
</a>
</p>
页:
[1]