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,038

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


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

 
Topic : 112487



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



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



คือผมดึงข้อมูลจาก ดาต้าเบสผ่าน json ข้อมูลมาแล้ว แต่!!! error ตรงนี้ครับ -[__NSDictionaryI name]: unrecognized selector sent to instance 0x7985c9e0 งงมากไม่รู้จะแก้ยังไงCode (Objective-C)
01.-(void) MyData
02.{
03.     
04.    imageid = @"ImageID";
05.    name = @"Name";
06.    path = @"Path";
07.    // And add a few recipes to it
08.    recipes = [NSMutableArray arrayWithCapacity:10];
09.    Recipe* recipe = [[Recipe alloc] init];
10.     
11.// ของเดิมเป็นแบบนี้ รันได้ไม่ติดปัญหา
12.//        recipe.name = @"Image 1";
13.//        recipe,path = @"http://absolutesoft.net/test/ios/col/img/img1.png";
14.//        recipe.image = [UIImage imageNamed:@"Image 2"];
15.//        [recipes addObject:recipe];
16.     
17.     
18.    //ของไหม่ติดปัญหา
19.        NSData *jsonData = [NSData dataWithContentsOfURL:
20.                            [NSURL URLWithString:@"http://absolutesoft.net/test/ios/col/getImages.php"]];
21.     
22.        id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
23.     
24.        // values in foreach loop
25.        for (NSDictionary *dataDict in jsonObjects) {
26.             
27.             
28.            recipe.image = [dataDict objectForKey:@"ImageID"];;
29.            recipe.name = [dataDict objectForKey:@"Name"];
30.            recipe.Path = [dataDict objectForKey:@"Path"];
31.            NSLog(@"recipe.image %@",recipe.image);
32.            NSLog(@"recipe.name %@",recipe.image);
33.            NSLog(@"recipe.Path %@",recipe.Path);
34.     
35.            dict = [NSDictionary dictionaryWithObjectsAndKeys:
36.                    recipe.image, imageid,
37.                    recipe.name, name,
38.                    recipe.Path, path,
39.                    nil];
40.            [recipes addObject:dict];
41.        }
42.        //NSLog(@"Dict = %@",dict);
43.    NSLog(@"recipes = %@",recipes);
44.     
45.     
46.     
47.     
48.     
49.    NSArray* array = [[NSUserDefaults standardUserDefaults] objectForKey:FAVORITES_KEY];
50.    favorites = [NSMutableArray arrayWithArray:array];
51.     
52.}




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 : 1283 Reply : 3
 

 

No. 1



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

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

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

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

Code (Objective-C)
01.- (void)connectionDidFinishLoading:(NSURLConnection *)connection
02.{
03.    if(receivedData)
04.    {
05.        //NSLog(@"%@",receivedData);
06.        //NSString *dataString = [[NSString alloc] initWithData:receivedData encoding:NSASCIIStringEncoding];
07.        //NSLog(@"%@",dataString);
08.         
09.        id jsonObjects = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableContainers error:nil];
10.         
11.        // values in foreach loop
12.        for (NSDictionary *dataDict in jsonObjects) {
13.            NSString *strGalleryID = [dataDict objectForKey:@"GalleryID"];
14.            NSString *strName = [dataDict objectForKey:@"Name"];
15.            NSString *strTitleName = [dataDict objectForKey:@"TitleName"];
16.            NSString *strThumbnail = [dataDict objectForKey:@"Thumbnail"];
17.             
18.            dict = [NSDictionary dictionaryWithObjectsAndKeys:
19.                    strGalleryID, galleryid,
20.                    strName, name,
21.                    strTitleName, titlename,
22.                    strThumbnail, thumbnail,
23.                    nil];
24.            [myObject addObject:dict];
25.        }
26.     
27.        [myTable reloadData];
28.    }
29.     
30.     
31.    // release the connection, and the data object
32.    [connection release];
33.    [receivedData release];
34.}


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)
01.-(void) MyData
02.{
03.    imageid = @"ImageID";
04.    name = @"Name";
05.    path = @"Path";
06.     
07.     
08.    // And add a few recipes to it
09.    recipes = [NSMutableArray arrayWithCapacity:10];
10.    Recipe* recipe = [[Recipe alloc] init];
11. 
12.     
13.     NSURL *searchURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://absolutesoft.net/test/ios/col/getImages.php"]];
14.     
15.     
16.    NSURLRequest *jSonRequest = [NSURLRequest requestWithURL:searchURL];
17.    [NSURLConnection sendAsynchronousRequest:jSonRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
18.         
19.        myObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&connectionError];
20.        recipes = [NSMutableArray arrayWithCapacity:myObject.count];
21.         
22.        for (NSDictionary *dataDict in myObject)
23.        {
24.             
25.             
26.            recipe.name = [dataDict objectForKey:@"Name"];
27.            recipe.image = [dataDict objectForKey:@"ImageID"];
28.            recipe.Path = [dataDict objectForKey:@"Path"];
29.     
30.            dict = [NSDictionary dictionaryWithObjectsAndKeys:recipe.name,name,recipe.image,imageid,recipe.Path,path,nil];
31.            [recipes addObject:dict];
32.             
33.        }
34.        NSLog(@"recipes = %@",recipes);
35.    }];
36.     
37.     
38. 
39.     
40.    NSArray* array = [[NSUserDefaults standardUserDefaults] objectForKey:FAVORITES_KEY];
41.    favorites = [NSMutableArray arrayWithArray:array];
42.     
43.}


ส่วนนี้คือไฟล์ที่เกี่ยวข้องครับ
DataModel.h
Code (Objective-C)
01.#import <Foundation/Foundation.h>
02.@class Recipe;
03. 
04./*
05. * The top-level data model object for our app.
06. */
07. 
08.@interface DataModel : NSObject
09. 
10. 
11.// DataModel is a singleton. You can access it with +sharedInstance.
12.+ (id)sharedInstance;
13. 
14.// Returns how many recipes are in the list.
15.- (int)recipeCount;
16. 
17.// Returns the Recipe object at the specified position in the list.
18.- (Recipe*)recipeAtIndex:(int)index;
19. 
20.// Deletes a Recipe object from the list.
21.- (void)removeRecipeAtIndex:(int)index;
22. 
23. 
24. 
25. 
26. 
27. 
28. 
29. 
30.// Returns the number of favorites.
31.- (int)favoritesCount;
32. 
33.// Returns the Recipe object at the specified position in the favorites list.
34.- (Recipe*)favoriteAtIndex:(int)index;
35. 
36.- (void)removeFavoriteAtIndex:(int)index;
37.// Determines whether a Recipe is in the list of favorites.
38.- (BOOL)isFavorite:(Recipe*)recipe;
39. 
40.// Adds a Recipe to the favorites.
41.- (void)addToFavorites:(Recipe*)recipe;
42. 
43.// Removes a Recipe from the favorites.
44.- (void)removeFromFavorites:(Recipe*)recipe;


DataModa.m
Code (Objective-C)
001.#import "DataModel.h"
002.#import "Recipe.h"
003. 
004. 
005.static NSString* const FAVORITES_KEY = @"Favorites";
006. 
007.@interface DataModel ()
008.{
009.     
010.    NSArray *myObject;
011. 
012.     
013. 
014.    NSString * name;
015.    NSString * imageid;
016.    NSString * path;
017. 
018.     
019.     
020.}
021. 
022. 
023.// The list of Recipe objects
024.@property (nonatomic, copy) NSMutableArray* recipes;
025.@property (nonatomic, retain) NSMutableArray* favorites;
026.@property (nonatomic,retain) NSDictionary* dict;
027.// The list of favorite Recipe objects
028. 
029.@end
030. 
031.@implementation DataModel
032. 
033.@synthesize recipes,favorites,dict;
034.+ (id)sharedInstance
035.{
036.    static DataModel* instance = nil;
037.    if (instance == nil)
038.    {
039.        instance = [[self alloc] init];
040.    }
041.    return instance;
042.}
043. 
044.- (id)init
045.{
046.    if ((self = [super init]))
047.    {
048.         
049.        [self MyData];
050. 
051.    }
052.    return self;
053.}
054. 
055. 
056. 
057.-(void) MyData
058.{
059.    imageid = @"ImageID";
060.    name = @"Name";
061.    path = @"ImageID";
062.     
063.     
064.    // And add a few recipes to it
065.    recipes = [NSMutableArray arrayWithCapacity:10];
066.    Recipe* recipe = [[Recipe alloc] init];
067. 
068.     
069.     NSURL *searchURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://absolutesoft.net/test/ios/col/getImages.php"]];
070.     
071.     
072.    NSURLRequest *jSonRequest = [NSURLRequest requestWithURL:searchURL];
073.    [NSURLConnection sendAsynchronousRequest:jSonRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
074.         
075.        myObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&connectionError];
076.        recipes = [NSMutableArray arrayWithCapacity:myObject.count];
077.         
078.        for (NSDictionary *dataDict in myObject)
079.        {
080.             
081.             
082.            recipe.name = [dataDict objectForKey:@"Name"];
083.            recipe.image = [dataDict objectForKey:@"ImageID"];
084.            recipe.Path = [dataDict objectForKey:@"Path"];
085.     
086.            dict = [NSDictionary dictionaryWithObjectsAndKeys:recipe.name,name,recipe.image,imageid,recipe.Path,path,nil];
087.            [recipes addObject:dict];
088.             
089.        }
090.        NSLog(@"recipes = %@",recipes);
091.    }];
092.     
093.     
094. 
095.     
096.    NSArray* array = [[NSUserDefaults standardUserDefaults] objectForKey:FAVORITES_KEY];
097.    favorites = [NSMutableArray arrayWithArray:array];
098.     
099.}
100. 
101.- (int)recipeCount
102.{
103.    return self.recipes.count;
104.}
105. 
106.- (Recipe*)recipeAtIndex:(int)index
107.{
108.     
109.    return [self.recipes objectAtIndex:index];
110. 
111.}
112. 
113.- (void)removeRecipeAtIndex:(int)index
114.{
115.    [self.recipes removeObjectAtIndex:index];
116.}
117. 
118. 
119.- (Recipe*)findRecipeNamed:(NSString*)name_
120.{
121.    for (Recipe* recipe in self.recipes)
122.    {
123.        if ([recipe.name isEqualToString:name_])
124.            return recipe;
125.    }
126.    return nil;
127.}
128. 
129. 
130. 
131. 
132. 
133. 
134. 
135.// Function เกี่ยวกับfavorites
136. 
137.- (int)favoritesCount
138.{
139.    return self.favorites.count;
140.}
141. 
142.- (Recipe*)favoriteAtIndex:(int)index
143.{
144.    NSString* name_ = [self.favorites objectAtIndex:index];
145.    return [self findRecipeNamed:name_];
146.}
147. 
148. 
149.- (void)removeFavoriteAtIndex:(int)index
150.{
151.    [self.favorites  removeObjectAtIndex:index];
152. 
153. 
154.    //ลบข้อมูลFavorite ที่เก็บลง memory
155. 
156.    [[NSUserDefaults standardUserDefaults] setObject:self.favorites forKey:FAVORITES_KEY];
157.    [[NSUserDefaults standardUserDefaults] synchronize];
158. 
159. 
160.}
161. 
162. 
163. 
164. 
165.- (BOOL)isFavorite:(Recipe*)recipe
166.{
167.    return [self.favorites containsObject:recipe.name];
168.}
169. 
170.- (void)addToFavorites:(Recipe*)recipe
171.{
172.    [self.favorites addObject:recipe.name];
173.    [[NSUserDefaults standardUserDefaults] setObject:self.favorites forKey:FAVORITES_KEY];
174.    [[NSUserDefaults standardUserDefaults] synchronize];
175.}
176. 
177.- (void)removeFromFavorites:(Recipe*)recipe
178.{
179.    [self.favorites removeObject:recipe.name];
180.    [[NSUserDefaults standardUserDefaults] setObject:self.favorites forKey:FAVORITES_KEY];
181.    [[NSUserDefaults standardUserDefaults] synchronize];
182.}


Recipe.h
Code (Objective-C)
1.#import <Foundation/Foundation.h>
2. 
3.@interface Recipe : NSObject
4. 
5.@property (nonatomic, copy) NSString* name;
6.@property (nonatomic, copy) NSString* Path;
7.@property (nonatomic, retain) UIImage* image;



Recipe.m
Code (Objective-C)
01.#import "Recipe.h"
02. 
03.@implementation Recipe
04.@synthesize name, image,Path;
05. 
06.- (NSString*)description
07.{
08.    return [NSString stringWithFormat:@"%@Name %@ImageID %@Path %@", [super description], self.name,self.image,self.Path];
09.     
10.}
11. 
12.@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,059 )
บทความ ( 838 )

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

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

Code (Objective-C)
1.[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 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)





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