1
我試圖掌握如何在目標C中使用塊。在這個簡單的應用程序,我正在寫,我得到一組圖像作爲一個JSON流。我試圖讓圖像出來並將它們物化,然後將它們收集到NSMutableArray
中。下面是我的代碼無法從JSON蒸汽檢索對象來枚舉
WebImage.h
#import <Foundation/Foundation.h>
@interface WebImage : NSObject <NSCopying>
@property (strong, nonatomic) NSString *imageId;
@property (strong, nonatomic) NSString *imageName;
@property (strong, nonatomic) NSString *imagePath;
- (id)initWithId:(NSString *)imageId andName:(NSString *)imageName andPath:(NSString *)imagePath;
+ (NSArray *)retrieveAllImages;
@end
WebImage.m
#import "WebImage.h"
#import "AFNetworking.h"
#define HOST_URL @"http://toonmoodz.osmium.lk/"
@implementation WebImage
#pragma mark - NSCopying
- (id)copyWithZone:(NSZone *)zone
{
WebImage *newImage = [WebImage new];
newImage.imageId = self.imageId;
newImage.imageName = self.imageName;
newImage.imagePath = self.imagePath;
return newImage;
}
- (id)initWithId:(NSString *)imageId andName:(NSString *)imageName andPath:(NSString *)imagePath
{
if (self = [self init]) {
self.imageId = imageId;
self.imageName = imageName;
self.imagePath = imagePath;
}
return self;
}
+ (NSArray *)retrieveAllImages
{
NSMutableArray *images = [[NSMutableArray alloc] init];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:[NSString stringWithFormat:@"%@%@", HOST_URL, @"All_Images.php"] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dict = responseObject[@"Images"];
NSArray *arr = [dict allValues];
[arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSDictionary *image = obj;
NSLog(@"%@", image);
[images addObject:[[WebImage alloc] initWithId:[image objectForKey:@"id"]
andName:[image objectForKey:@"name"]
andPath:[image objectForKey:@"image"]]];
}];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error in image retrieveing: %@", error.localizedDescription);
}];
return [images copy];
}
傳入的JSON流看起來像這樣
{
Images = (
{
createddate = "2014-01-08 12:20:24";
id = 1;
image = "images/Rock_1.png";
isEnabled = 1;
name = "Rock_1";
},
{
createddate = "2014-01-08 15:12:26";
id = 2;
image = "images/Rock_2.png";
isEnabled = 1;
name = "Rock_2";
}
);
}
我的問題是我無法檢索來自JSON字符串的圖像對象。當我調試時,編譯器甚至不會進入retrieveAllImages
方法中的GET塊,而是直接跳轉到return語句。
任何人都可以請告訴我如何糾正?
我創建了一個項目,演示了我正面臨的問題,並且我已經上傳了它here如果您想快速瀏覽一下。
謝謝。
您是否嘗試在'GET'塊中設置斷點**?我認爲如果你從外部接近它們,調試器會跳過塊。 – hgwhittle
我只是試過,但沒有。它仍然跳過該塊。 – Isuru
它也沒有進入故障模塊? – hgwhittle