Ipad 如何以键值形式更改来自服务器的值?

Ipad 如何以键值形式更改来自服务器的值?,ipad,Ipad,我想更改来自web服务器的特定密钥的值。 我的代码是这样的 NSMutableArray *forumTopicData = [[NSMutableArray alloc] init]; NSArray *columnArrayNames = [NSArray arrayWithObjects:@"user_id",@"class_id",@"role_id", @"first_name",@"last_name",@"photo_image_url",@"email_addres

我想更改来自web服务器的特定密钥的值。 我的代码是这样的

    NSMutableArray *forumTopicData = [[NSMutableArray alloc] init];

NSArray *columnArrayNames = [NSArray arrayWithObjects:@"user_id",@"class_id",@"role_id",   @"first_name",@"last_name",@"photo_image_url",@"email_address",@"webpage",@"home_phone",@"office_phone",@"cell_phone",@"aboutme",@"gender",@"is_online",@"role_title",@"display_name",nil];
[tempDict setObject:tempValue forKey:[columnArrayNames objectAtIndex:i]];                  //storing in array after getting from sqlite table


            [forumTopicData addObject:tempDict];

有人能告诉我如何操作来自服务器的密钥-值对的值吗?

您不想在应用程序中更改密钥。在服务器端的开发过程中,密钥名称可以随时更改,当它们更改时,您的代码将中断,您将发现自己正在搜索应用程序代码,试图修复每个密钥名称

正如Yogi雄辩地指出的那样,您希望创建一个从
NSObject
类继承的类,其中将有映射到键的属性。可以通过此类的对象表示每个实体

这是怎么做的

第1步: 创建自定义数据模型,使数据管理更简单、更清晰。 创建两个名为
ForumTopicDataModel.h
ForumTopicDataModel.m
的新文件。它们将是
NSObject
类的子类

这是您的
.h
文件的外观:

#import <Foundation/Foundation.h>

@interface ForumTopicDataModel : NSObject


/*
@"user_id", @"class_id", @"role_id", @"first_name", @"last_name", @"photo_image_url",
@"email_address", @"webpage", @"home_phone", @"office_phone", @"cell_phone", 
@"aboutme", @"gender", @"is_online", @"role_title", @"display_name"
*/

/*
Notice the naming convention used with a prefix of the class name. This make it easy to see all the properties of this particular class during text autocompletion when you are programming.
*/
@property (assign) NSInteger userId;
@property (assign) NSInteger userRoleId;
@property (nonatomic, retain) NSString * userFirstName;
//Do the same for the rest of the keys that you have.


-(id)initWithJSONData:(NSDictionary*)data;
@end
我们这样做是因为这将包含易于使用的容器。这也是管理自定义json对象的好方法

步骤2 现在,在要填充数据库并对数据进行分类的视图控制器中,确保导入自定义的
ForumTopicDataModel.h
文件

NSMutableDictionary *myData = [[NSMutableDictionary alloc] init];

//Have a for loop that iterates through your array
for(NSDictionary *currentItem in serverArray){
    ForumTopicDataModel *newItem = [[ForumTopicDataModel alloc] initWithJSONData:currentItem];

    [myForumsArray addObject:newItem];
}
现在,每当需要访问特定论坛的特定属性时,只需访问希望从ForumTopicDataModel对象检索的属性值的属性名称。这样做的美妙之处在于它保持了所有代码的整洁,并且使代码易于调试

就这些,兄弟。祝你好运


免责声明,我还没有测试过这段代码,但这应该可以立即运行:)

你不想这样做。在开发过程中,密钥名称可以随时更改,当它们更改时,您的代码将中断,您将发现自己正在搜索应用程序代码,试图修复每个密钥名称。相反,您要做的是创建
NSObject
类型的数据模型。在这里,您只需将每个键映射到每个属性,然后在整个应用程序中使用此数据模型,甚至可以从此数据模型类创建对象并在数据源数组中使用。您能告诉我如何使用此NSManagedObjectModel吗?我从来没有说过
NSManagedObjectModel
。哦,对不起,NSObject的数据模型。你能举个例子吗?我认为Pavan的意思是创建一个从NSObject类继承的新类,其中将有属性映射到键。可以通过此类的对象表示每个实体。
NSMutableDictionary *myData = [[NSMutableDictionary alloc] init];

//Have a for loop that iterates through your array
for(NSDictionary *currentItem in serverArray){
    ForumTopicDataModel *newItem = [[ForumTopicDataModel alloc] initWithJSONData:currentItem];

    [myForumsArray addObject:newItem];
}