iOS -- 讀取plist表中資料到NSArray
阿新 • • 發佈:2019-01-02
將下表的資料列印輸出來
上程式碼
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) NSArray * nameDict;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self print];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (NSArray *)nameDict
{
if (!_nameDict) {
NSString * path = [[NSBundle mainBundle] pathForResource:@"nameData.plist" ofType:nil];
_nameDict = [NSArray arrayWithContentsOfFile:path];
}
return _nameDict;
}
- (void) print
{
for (int i = 0; i < self.nameDict.count; i++) {
NSLog (@"name:%@,age:%@",self.nameDict[i][@"name"],self.nameDict[i][@"age"]);
}
}
@end
執行結果:
完美列印。