Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 未加载来自plist的数据_Iphone_Ios_Objective C - Fatal编程技术网

Iphone 未加载来自plist的数据

Iphone 未加载来自plist的数据,iphone,ios,objective-c,Iphone,Ios,Objective C,有人能帮我解决这个问题吗。我想使用nsobject的属性列表开发我的应用程序。运行应用程序时,没有错误,但未在uitableview上加载来自bnm.plist的数据 下面是ViewController.m #import "MCWViewController.h" #import "MCWPlacesDetailViewController.h" #import "MCWPlaces.h" @interface MCWViewController () @end @implementat

有人能帮我解决这个问题吗。我想使用nsobject的属性列表开发我的应用程序。运行应用程序时,没有错误,但未在uitableview上加载来自bnm.plist的数据

下面是ViewController.m

#import "MCWViewController.h"
#import "MCWPlacesDetailViewController.h"
#import "MCWPlaces.h"

@interface MCWViewController ()

@end

@implementation MCWViewController {
    NSArray *places;
}

@synthesize tableView = _tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Initialize table data

    // Find out the path of bnm.plist
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bnm" ofType:@"plist"];

    // Load the file content and read the data into arrays
    NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
    MCWPlaces *placeGO = [MCWPlaces new];
    placeGO.name = [dict objectForKey:@"PlaceName"];
    placeGO.info = [dict objectForKey:@"PlaceInfo"];

    places = [NSArray arrayWithObject:placeGO];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [places count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"PlacesCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSLog(@"PlacesCell");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    MCWPlaces *place = [places objectAtIndex:indexPath.row];
    cell.textLabel.text = place.name;
    return cell;


}


- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([segue.identifier isEqualToString:@"showPlaceDetails"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        MCWPlacesDetailViewController *destViewController = segue.destinationViewController;
        destViewController.place = [places objectAtIndex:indexPath.row];
    }
}
下面是地点

#import <Foundation/Foundation.h>

@interface MCWPlaces : NSObject


@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *info;

@end
下面是详细信息

#import "MCWPlacesDetailViewController.h"


@interface MCWPlacesDetailViewController ()

@end

@implementation MCWPlacesDetailViewController 

@synthesize placeInfo;
@synthesize place;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = place.name;
    self.placeInfo.text = place.info;

}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
以下是详细信息

#import <UIKit/UIKit.h>
#import "MCWPlaces.h"

@interface MCWPlacesDetailViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *placeInfo;

@property (nonatomic, strong) MCWPlaces *place;



@end
#导入
#导入“MCWPlaces.h”
@接口MCWPlacesDetailViewController:UIViewController
@属性(弱、非原子)IBUILabel*placeInfo;
@不动产(非原子、强)MCWPlaces*场所;
@结束
普利斯特

<plist version="1.0">
<dict>
    <key>PlaceName</key>
    <array>
        <string>Place 1</string>
        <string>Place 2</string>
        <string>Place 3</string>
    </array>
    <key>PlaceInfo</key>
    <array>
        <string>Place 1 Info</string>
        <string>Place 2 Info</string>
        <string>Place 3 Info</string>
    </array>
</dict>
</plist>

地名
地点1
地点2
地点3
PlaceInfo
位置1信息
地点2信息
地点3信息
我的代码有问题吗


我想我的问题是'CellForRowatineXpath',我已经在Places.h上将'name'和'info'声明为nsstring。但在plist上,它就像nsarray一样。但是我不知道如何解决这个问题。

我认为您在加载数据时遇到了问题

首先,您说您从place.plist获取数据,但在viewDidLoad中,您试图从bnm.plist加载数据

请尝试使用以下代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Initialize table data

    // Find out the path of bnm.plist
    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bnm" ofType:@"plist"]];

    // Load the file content and read the data into arrays
    MCWPlaces *placeGO = [MCWPlaces new];
    placeGO.name = [dict objectForKey:@"PlaceName"];
    placeGO.info = [dict objectForKey:@"PlaceInfo"];
}

您能给我们看一下您的bnm.plist吗?

好的,那么您走错了。 使用以下代码并在项目中替换它:

bnm.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>PlaceName</key>
        <string>Place 1</string>
        <key>PlaceInfo</key>
        <string>Place 1 Info</string>
    </dict>
    <dict>
        <key>PlaceName</key>
        <string>Place 2</string>
        <key>PlaceInfo</key>
        <string>Place 2 Info</string>
    </dict>
</array>
</plist>
MCWPlaces.m

#import "MCWPlaces.h"

@implementation MCWPlaces

@synthesize name;
@synthesize info;

@end
#import "MCWViewController.h"
#import "MCWPlaces.h"

#define KEY_PLACE_NAME  @"PlaceName"
#define KEY_PLACE_INFO  @"PlaceInfo"

@interface MCWViewController ()

@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *places;

@end


@implementation MCWViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Init place array
    _places = [NSMutableArray array];

    // Find out the path of bnm.plist
    NSArray *data = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bnm" ofType:@"plist"]];

    NSLog(@"%@", data);

    for (NSInteger i = 0; i < data.count; i++)
    {
        MCWPlaces *placeGO = [MCWPlaces new];

        placeGO.name = [[data objectAtIndex:i] objectForKey:KEY_PLACE_NAME];
        placeGO.info = [[data objectAtIndex:i] objectForKey:KEY_PLACE_INFO];

        [_places addObject:placeGO];
    }

    NSLog(@"%@", _places);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _places.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"PlacesCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSLog(@"PlacesCell");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }



    NSLog(@"%@", [_places objectAtIndex:indexPath.row]);

    cell.textLabel.text = [[_places objectAtIndex:indexPath.row] name];
    return cell;


}

@end
MCWViewController.m

#import "MCWPlaces.h"

@implementation MCWPlaces

@synthesize name;
@synthesize info;

@end
#import "MCWViewController.h"
#import "MCWPlaces.h"

#define KEY_PLACE_NAME  @"PlaceName"
#define KEY_PLACE_INFO  @"PlaceInfo"

@interface MCWViewController ()

@property (nonatomic, weak) IBOutlet UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *places;

@end


@implementation MCWViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Init place array
    _places = [NSMutableArray array];

    // Find out the path of bnm.plist
    NSArray *data = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"bnm" ofType:@"plist"]];

    NSLog(@"%@", data);

    for (NSInteger i = 0; i < data.count; i++)
    {
        MCWPlaces *placeGO = [MCWPlaces new];

        placeGO.name = [[data objectAtIndex:i] objectForKey:KEY_PLACE_NAME];
        placeGO.info = [[data objectAtIndex:i] objectForKey:KEY_PLACE_INFO];

        [_places addObject:placeGO];
    }

    NSLog(@"%@", _places);
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _places.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"PlacesCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        NSLog(@"PlacesCell");
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }



    NSLog(@"%@", [_places objectAtIndex:indexPath.row]);

    cell.textLabel.text = [[_places objectAtIndex:indexPath.row] name];
    return cell;


}

@end
#导入“MCWViewController.h”
#导入“MCWPlaces.h”
#定义关键字位置名称@“PlaceName”
#定义关键字位置信息@“位置信息”
@接口MCWViewController()
@属性(非原子,弱)IBUITableView*tableView;
@属性(非原子,强)NSMutableArray*位置;
@结束
@MCWViewController的实现
-(无效)viewDidLoad
{
[超级视图下载];
//初始位置数组
_places=[NSMutableArray];
//找出bnm.plist的路径
NSArray*数据=[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@“bnm”类型:@“plist”];
NSLog(@“%@”,数据);
对于(NSInteger i=0;i
文件在哪里?在捆绑包中还是在
文档
文件夹中?因为它位于
Documents
文件夹中,所以方法
–pathForResource:ofType:
不会为它提供路径…@jordon,对不起,实际上plist文件是bnm.plist,我已经插入了plist。当我放置“places=[NSArray arrayWithObject:placeGO];”并运行应用程序时,输出“-[\u NSCFArray IsequalString:]:未识别的选择器发送到实例0x75992e0”,应用程序将在“cell.textLabel.text=place.name;”处中断@乔登,我试过你建议的代码,但都是一样的。我想我的问题是'CellForRowatineXpath',我已经在Places.h上将'name'和'info'声明为nsstring。但在plist上,它就像nsarray一样。但是我不知道如何解决这个问题。请分享你的plist文件的结构。@shhrzl尝试我的第二个答案;)这是工作..非常感谢..从您的代码中,我添加了“-(void)prepareforsgue:(UIStoryboardSegue*)segue sender:(id)sender”以加载PlaceInfo