iOS - สอบถามเรื่องดึงข้อมูลแบบ Json ครับผมทำแล้วแต่ติดตรง -[__NSDictionaryI name]:
คือผมดึงข้อมูลจาก ดาต้าเบสผ่าน json ข้อมูลมาแล้ว แต่!!! error ตรงนี้ครับ -[__NSDictionaryI name]: unrecognized selector sent to instance 0x7985c9e0 งงมากไม่รู้จะแก้ยังไงCode (Objective-C)
-(void) MyData
{
imageid = @"ImageID";
name = @"Name";
path = @"Path";
// And add a few recipes to it
recipes = [NSMutableArray arrayWithCapacity:10];
Recipe* recipe = [[Recipe alloc] init];
// ของเดิมเป็นแบบนี้ รันได้ไม่ติดปัญหา
// recipe.name = @"Image 1";
// recipe,path = @"http://absolutesoft.net/test/ios/col/img/img1.png";
// recipe.image = [UIImage imageNamed:@"Image 2"];
// [recipes addObject:recipe];
//ของไหม่ติดปัญหา
NSData *jsonData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://absolutesoft.net/test/ios/col/getImages.php"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
// values in foreach loop
for (NSDictionary *dataDict in jsonObjects) {
recipe.image = [dataDict objectForKey:@"ImageID"];;
recipe.name = [dataDict objectForKey:@"Name"];
recipe.Path = [dataDict objectForKey:@"Path"];
NSLog(@"recipe.image %@",recipe.image);
NSLog(@"recipe.name %@",recipe.image);
NSLog(@"recipe.Path %@",recipe.Path);
dict = [NSDictionary dictionaryWithObjectsAndKeys:
recipe.image, imageid,
recipe.name, name,
recipe.Path, path,
nil];
[recipes addObject:dict];
}
//NSLog(@"Dict = %@",dict);
NSLog(@"recipes = %@",recipes);
NSArray* array = [[NSUserDefaults standardUserDefaults] objectForKey:FAVORITES_KEY];
favorites = [NSMutableArray arrayWithArray:array];
}
Tag : Mobile, iOS, iPod, iPhone, iPad, Objective-C
Date :
2014-11-11 15:36:21
By :
iMonster
View :
1225
Reply :
3
ลองแบบนี้ได้ไหม๊ครับ
Code (Objective-C)
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
if(receivedData)
{
//NSLog(@"%@",receivedData);
//NSString *dataString = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
//NSLog(@"%@",dataString);
id jsonObjects = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:nil];
// values in foreach loop
for (NSDictionary *dataDict in jsonObjects) {
NSString *strGalleryID = [dataDict objectForKey:@"GalleryID"];
NSString *strName = [dataDict objectForKey:@"Name"];
NSString *strTitleName = [dataDict objectForKey:@"TitleName"];
NSString *strThumbnail = [dataDict objectForKey:@"Thumbnail"];
dict = [NSDictionary dictionaryWithObjectsAndKeys:
strGalleryID, galleryid,
strName, name,
strTitleName, titlename,
strThumbnail, thumbnail,
nil];
[myObject addObject:dict];
}
[myTable reloadData];
}
// release the connection, and the data object
[connection release];
[receivedData release];
}
iOS/iPhone NSURLConnection and PHP MySQL / JSON (TableView,UITableView)
Date :
2014-11-11 16:20:16
By :
mr.win
ขอบคุณครับลอง เอามาประยุคดูแล้วโอเคไม่ติด error เลย ข้อมูลเก็บไว้ที่ recipes มีข้อมูลเรียบร้อย
แต่!! TableView แสดงข้อมูลว่างป่าว ครับ... งงอีกรอบ
Code (Objective-C)
-(void) MyData
{
imageid = @"ImageID";
name = @"Name";
path = @"Path";
// And add a few recipes to it
recipes = [NSMutableArray arrayWithCapacity:10];
Recipe* recipe = [[Recipe alloc] init];
NSURL *searchURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://absolutesoft.net/test/ios/col/getImages.php"]];
NSURLRequest *jSonRequest = [NSURLRequest requestWithURL:searchURL];
[NSURLConnection sendAsynchronousRequest:jSonRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
myObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&connectionError];
recipes = [NSMutableArray arrayWithCapacity:myObject.count];
for (NSDictionary *dataDict in myObject)
{
recipe.name = [dataDict objectForKey:@"Name"];
recipe.image = [dataDict objectForKey:@"ImageID"];
recipe.Path = [dataDict objectForKey:@"Path"];
dict = [NSDictionary dictionaryWithObjectsAndKeys:recipe.name,name,recipe.image,imageid,recipe.Path,path,nil];
[recipes addObject:dict];
}
NSLog(@"recipes = %@",recipes);
}];
NSArray* array = [[NSUserDefaults standardUserDefaults] objectForKey:FAVORITES_KEY];
favorites = [NSMutableArray arrayWithArray:array];
}
ส่วนนี้คือไฟล์ที่เกี่ยวข้องครับ
DataModel.h
Code (Objective-C)
#import <Foundation/Foundation.h>
@class Recipe;
/*
* The top-level data model object for our app.
*/
@interface DataModel : NSObject
// DataModel is a singleton. You can access it with +sharedInstance.
+ (id)sharedInstance;
// Returns how many recipes are in the list.
- (int)recipeCount;
// Returns the Recipe object at the specified position in the list.
- (Recipe*)recipeAtIndex:(int)index;
// Deletes a Recipe object from the list.
- (void)removeRecipeAtIndex:(int)index;
// Returns the number of favorites.
- (int)favoritesCount;
// Returns the Recipe object at the specified position in the favorites list.
- (Recipe*)favoriteAtIndex:(int)index;
- (void)removeFavoriteAtIndex:(int)index;
// Determines whether a Recipe is in the list of favorites.
- (BOOL)isFavorite:(Recipe*)recipe;
// Adds a Recipe to the favorites.
- (void)addToFavorites:(Recipe*)recipe;
// Removes a Recipe from the favorites.
- (void)removeFromFavorites:(Recipe*)recipe;
DataModa.m
Code (Objective-C)
#import "DataModel.h"
#import "Recipe.h"
static NSString* const FAVORITES_KEY = @"Favorites";
@interface DataModel ()
{
NSArray *myObject;
NSString * name;
NSString * imageid;
NSString * path;
}
// The list of Recipe objects
@property (nonatomic, copy) NSMutableArray* recipes;
@property (nonatomic, retain) NSMutableArray* favorites;
@property (nonatomic,retain) NSDictionary* dict;
// The list of favorite Recipe objects
@end
@implementation DataModel
@synthesize recipes,favorites,dict;
+ (id)sharedInstance
{
static DataModel* instance = nil;
if (instance == nil)
{
instance = [[self alloc] init];
}
return instance;
}
- (id)init
{
if ((self = [super init]))
{
[self MyData];
}
return self;
}
-(void) MyData
{
imageid = @"ImageID";
name = @"Name";
path = @"ImageID";
// And add a few recipes to it
recipes = [NSMutableArray arrayWithCapacity:10];
Recipe* recipe = [[Recipe alloc] init];
NSURL *searchURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://absolutesoft.net/test/ios/col/getImages.php"]];
NSURLRequest *jSonRequest = [NSURLRequest requestWithURL:searchURL];
[NSURLConnection sendAsynchronousRequest:jSonRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
myObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&connectionError];
recipes = [NSMutableArray arrayWithCapacity:myObject.count];
for (NSDictionary *dataDict in myObject)
{
recipe.name = [dataDict objectForKey:@"Name"];
recipe.image = [dataDict objectForKey:@"ImageID"];
recipe.Path = [dataDict objectForKey:@"Path"];
dict = [NSDictionary dictionaryWithObjectsAndKeys:recipe.name,name,recipe.image,imageid,recipe.Path,path,nil];
[recipes addObject:dict];
}
NSLog(@"recipes = %@",recipes);
}];
NSArray* array = [[NSUserDefaults standardUserDefaults] objectForKey:FAVORITES_KEY];
favorites = [NSMutableArray arrayWithArray:array];
}
- (int)recipeCount
{
return self.recipes.count;
}
- (Recipe*)recipeAtIndex:(int)index
{
return [self.recipes objectAtIndex:index];
}
- (void)removeRecipeAtIndex:(int)index
{
[self.recipes removeObjectAtIndex:index];
}
- (Recipe*)findRecipeNamed:(NSString*)name_
{
for (Recipe* recipe in self.recipes)
{
if ([recipe.name isEqualToString:name_])
return recipe;
}
return nil;
}
// Function เกี่ยวกับfavorites
- (int)favoritesCount
{
return self.favorites.count;
}
- (Recipe*)favoriteAtIndex:(int)index
{
NSString* name_ = [self.favorites objectAtIndex:index];
return [self findRecipeNamed:name_];
}
- (void)removeFavoriteAtIndex:(int)index
{
[self.favorites removeObjectAtIndex:index];
//ลบข้อมูลFavorite ที่เก็บลง memory
[[NSUserDefaults standardUserDefaults] setObject:self.favorites forKey:FAVORITES_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (BOOL)isFavorite:(Recipe*)recipe
{
return [self.favorites containsObject:recipe.name];
}
- (void)addToFavorites:(Recipe*)recipe
{
[self.favorites addObject:recipe.name];
[[NSUserDefaults standardUserDefaults] setObject:self.favorites forKey:FAVORITES_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)removeFromFavorites:(Recipe*)recipe
{
[self.favorites removeObject:recipe.name];
[[NSUserDefaults standardUserDefaults] setObject:self.favorites forKey:FAVORITES_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Recipe.h
Code (Objective-C)
#import <Foundation/Foundation.h>
@interface Recipe : NSObject
@property (nonatomic, copy) NSString* name;
@property (nonatomic, copy) NSString* Path;
@property (nonatomic, retain) UIImage* image;
Recipe.m
Code (Objective-C)
#import "Recipe.h"
@implementation Recipe
@synthesize name, image,Path;
- (NSString*)description
{
return [NSString stringWithFormat:@"%@Name %@ImageID %@Path %@", [super description], self.name,self.image,self.Path];
}
@end
ประวัติการแก้ไข 2014-11-11 20:22:06 2014-11-11 20:23:29
Date :
2014-11-11 20:20:45
By :
iMonster
Code (Objective-C)
[myTable reloadData];
คุณจะต้อง reloadData ด้วยครับ เพราะตัว Data ถูกโหลดหลังจากที่ Table มัน Render ไปแล้ว
Date :
2014-11-12 09:41:14
By :
mr.win
Load balance : Server 02