Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.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
if语句ios check json参数_Ios_Objective C_Json_Parsing - Fatal编程技术网

if语句ios check json参数

if语句ios check json参数,ios,objective-c,json,parsing,Ios,Objective C,Json,Parsing,我目前正在分析iOS应用程序中我的表视图的JSON提要。在JSON提要中有两种不同类型的帖子:“type”:“post”和“type”:“shared” 当我点击表格视图中的一个单元格时,会打开一个包含完整帖子等内容的新视图。我需要的是在didSelectRowAtIndexPath中设置一个if语句,说:“如果type=post,打开此视图。否则如果type=shared,打开此视图”。因此,我需要以某种方式检查json参数类型。基于这一点,我们可以看到两种不同的观点 didSelectRow

我目前正在分析iOS应用程序中我的表视图的JSON提要。在JSON提要中有两种不同类型的帖子:
“type”:“post”
“type”:“shared”

当我点击表格视图中的一个单元格时,会打开一个包含完整帖子等内容的新视图。我需要的是在didSelectRowAtIndexPath中设置一个if语句,说:“如果type=post,打开此视图。否则如果type=shared,打开此视图”。因此,我需要以某种方式检查json参数类型。基于这一点,我们可以看到两种不同的观点

didSelectRowAtIndexPath方法当前如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //getting the data I'm passing to the next view, where movies = NSMutableArray 
    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];

    // Allocating the Second View I want to open
    HomePostsObject *newView = [[HomePostsObject alloc] init];

    // passing the data to newView
    newView.theMovie = movie;

    // presenting the view
    [self.navigationController pushViewController:newView animated:YES];
}
@implementation HomeViewController
@synthesize profilePic;
@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView;
@synthesize btnFaceBook, btnTwitter, btnTwitter2;
@synthesize strURLToLoad;
@synthesize movies;
@synthesize statsHome;
@synthesize fontForCellText;
@synthesize twitterFollowers;
@synthesize facebookLikes;
@synthesize instagramFollowers;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Initializing Data Source
    movies = [[NSMutableArray alloc] init];

    NSURL *url = [[NSURL alloc] initWithString:@"link.php"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.movies = [[NSArray alloc] initWithArray:JSON];
        [self.activityIndicatorView stopAnimating];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return movies.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}

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

    HomeFeedView *cell = (HomeFeedView *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HomeFeedView" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.backgroundColor = [UIColor clearColor];

    }

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];

    cell.title.text = [movie objectForKey:@"message"];
    cell.published.text = [movie objectForKey:@"published"];
    cell.twitterName.text = [movie objectForKey:@"celebname"];

    return cell;
}

- (NSString *)getTextKey
{
    return @"message";
}

- (NSString *)getPostedTime
{
    return @"published";
}

- (NSString *)getTwitterName
{
    return @"celebname";
}

- (CGFloat)getHeightForText:(NSString *)strText
{
    CGSize constraintSize = CGSizeMake(cellTextWidth, MAXFLOAT);
    CGSize labelSize = [strText sizeWithFont:fontForCellText constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"labelSize.height = %f",labelSize.height);
    return labelSize.height;
}
@end
解析数据的完整代码如下所示:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //getting the data I'm passing to the next view, where movies = NSMutableArray 
    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];

    // Allocating the Second View I want to open
    HomePostsObject *newView = [[HomePostsObject alloc] init];

    // passing the data to newView
    newView.theMovie = movie;

    // presenting the view
    [self.navigationController pushViewController:newView animated:YES];
}
@implementation HomeViewController
@synthesize profilePic;
@synthesize tableView = _tableView, activityIndicatorView = _activityIndicatorView;
@synthesize btnFaceBook, btnTwitter, btnTwitter2;
@synthesize strURLToLoad;
@synthesize movies;
@synthesize statsHome;
@synthesize fontForCellText;
@synthesize twitterFollowers;
@synthesize facebookLikes;
@synthesize instagramFollowers;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Initializing Data Source
    movies = [[NSMutableArray alloc] init];

    NSURL *url = [[NSURL alloc] initWithString:@"link.php"];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];

    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        self.movies = [[NSArray alloc] initWithArray:JSON];
        [self.activityIndicatorView stopAnimating];
        [self.tableView reloadData];

    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];

    [operation start];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return movies.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 85;
}

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

    HomeFeedView *cell = (HomeFeedView *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HomeFeedView" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.backgroundColor = [UIColor clearColor];

    }

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];

    cell.title.text = [movie objectForKey:@"message"];
    cell.published.text = [movie objectForKey:@"published"];
    cell.twitterName.text = [movie objectForKey:@"celebname"];

    return cell;
}

- (NSString *)getTextKey
{
    return @"message";
}

- (NSString *)getPostedTime
{
    return @"published";
}

- (NSString *)getTwitterName
{
    return @"celebname";
}

- (CGFloat)getHeightForText:(NSString *)strText
{
    CGSize constraintSize = CGSizeMake(cellTextWidth, MAXFLOAT);
    CGSize labelSize = [strText sizeWithFont:fontForCellText constrainedToSize:constraintSize lineBreakMode:NSLineBreakByWordWrapping];
    NSLog(@"labelSize.height = %f",labelSize.height);
    return labelSize.height;
}
@end
JSON结构/数据:

{
      "message":"blablablabla",
      "published":"5 hours ago",
      "link":"http://google.com",
      "unix":1395493814,
      "name":"Name",
      "story":null,
      "picture":"some-pic.jpg",
      "type":"shared"
   },
   {
      "message":"blablabla",
      "published":"5 hours ago",
      "name":"Name",
      "unix":1395493814,
      "type":"post"
   },

在您的HomeFeedView.h中,只需添加一个新属性,比如说type

@property(strong, nonatomic) NSString *type;
然后在cellForRowAtIndexPath委托方法中设置此类型属性

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

    HomeFeedView *cell = (HomeFeedView *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"HomeFeedView" owner:self options:nil];
        cell = [nib objectAtIndex:0];
        cell.backgroundColor = [UIColor clearColor];

    }

    NSDictionary *movie = [self.movies objectAtIndex:indexPath.row];

    cell.title.text = [movie objectForKey:@"message"];
    cell.published.text = [movie objectForKey:@"published"];
    cell.twitterName.text = [movie objectForKey:@"celebname"];
    cell.type = [movie objectForKey:@"type"];

    return cell;
}
然后写下这个:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //getting the data I'm passing to the next view, where movies = NSMutableArray 
    HomeFeedView *cell = [tableview cellForRowAtIndexPath:indexPath];
    if([cell.type isEqualToString: @"post"]){

    // Allocating the Second View I want to open
    HomePostsObject *newView = [[HomePostsObject alloc] init];

    // passing the data to newView
    newView.theMovie = [self.movies objectAtIndex:indexPath.row];

    // presenting the view
    [self.navigationController pushViewController:newView animated:YES];
   }
   else{

    // Allocating the Second View I want to open
    HomePostsObject *newView = [[HomePostsObject alloc] init];

    // passing the data to newView
    newView.theMovie = [self.movies objectAtIndex:indexPath.row];

    // presenting the view
    [self.navigationController pushViewController:newView animated:YES];
   }
}

“type”值在哪里,在JSON的主体中,还是在请求状态中?(给我们看一些实际的JSON。)检查我的编辑@hotlicksy你仍然保留着原始的JSON数组,为什么不干脆做
NSString type=self.movies[indexPath.row][@“type”]
在didSelectRowAtIndexPath中访问类型?是的,这就是我要做的