Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Mobile Forum > iOS - สอบถามเรื่องดึงข้อมูลแบบ Json ครับผมทำแล้วแต่ติดตรง -[__NSDictionaryI name]:



 

iOS - สอบถามเรื่องดึงข้อมูลแบบ Json ครับผมทำแล้วแต่ติดตรง -[__NSDictionaryI name]:

 



Topic : 112487



โพสกระทู้ ( 20 )
บทความ ( 0 )



สถานะออฟไลน์




คือผมดึงข้อมูลจาก ดาต้าเบสผ่าน 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







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2014-11-11 15:36:21 By : iMonster View : 1151 Reply : 3
 

 

No. 1



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

ลองแบบนี้ได้ไหม๊ครับ

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
 


 

No. 2



โพสกระทู้ ( 20 )
บทความ ( 0 )



สถานะออฟไลน์


ขอบคุณครับลอง เอามาประยุคดูแล้วโอเคไม่ติด 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
 

 

No. 3



โพสกระทู้ ( 74,058 )
บทความ ( 838 )

สมาชิกที่ใส่เสื้อไทยครีเอท

สถานะออฟไลน์
Twitter Facebook

Code (Objective-C)
[myTable reloadData];


คุณจะต้อง reloadData ด้วยครับ เพราะตัว Data ถูกโหลดหลังจากที่ Table มัน Render ไปแล้ว
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2014-11-12 09:41:14 By : mr.win
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : iOS - สอบถามเรื่องดึงข้อมูลแบบ Json ครับผมทำแล้วแต่ติดตรง -[__NSDictionaryI name]:
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 00
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่