This question already has answers here:
Convert an iOS objective c object to a JSON string
(4 answers)
Closed 9 years ago.
How can a NSObject class be sent through json post?
Example:
#interface TestClass : NSObject
#property (nonatomic,strong) NSString *Value1;
#property (nonatomic,strong) NSString *Value2;
#property (nonatomic,strong) NSString *Value1;
#end
In another file that implements the TestClass:
TestClass *test = [[TestClass alloc]init];
test.Value1 = #"First";
test.Value2 = #"Second";
test.Value3 = #"Third";
.....
How can the test object be sent?
As #Carl points out you have to convert it first to a JSON object, and then send it as a parameter on your POST request. Be aware that the back-end defines how your data should be send for requests. But this is the most common way of doing it.
Related
I use JSONModel to hold my app datasource, and use -(id)initWithArray:(NSArray *)array modelClass:(Class)cls generated an JSONModelArray, now I want to do some search stuff like enumerateObjectsUsingBlock: method does. But I found that JSONModelArray is not inherited from NSArray.
So, how can I do this?
Try use BWJSONMatcher to convert json string to a NSArray.
For example, your json string seems like :
[{"name":"Arron","age":20,"grade":2},{"name":"Burrows","age":21,"grade":2}]
All you have to do is declare your own data model:
#interface Student : NSObject
#property (nonatomic, strong) NSString *name;
#property (nonatomic, assign) NSInteger age;
#property (nonatomic, assign) NSInteger grade;
#end
BWJSONMatcher will help you convert it to a NSArray in a very neat way:
NSArray *students = [BWJSONMatcher matchJSON:jsonString withClass:[Student class]];
This question already has answers here:
ios/objective c/singleton: Storing userid in session variable
(3 answers)
Closed 7 years ago.
This should not be that hard but I cannot get it to work. IOS newb trying to set userid in session instance following the answer by Ismael here. But cannot access value of userid in other class.
Here is the code I am using:
Session.h
#interface IDSession : NSObject
#property (readonly, copy) NSString *userid;
+ (IDSession *)sharedInstance;
#end
Session.m
#import "IDSession.h"
#interface IDSession()
#property (readwrite,copy)NSString * userid;
#end
#implementation IDSession
+ (IDSession *)sharedInstance {
static IDSession *session;
if (!session){
session = [[IDSession alloc] init];
//include this class in other class and reference userid with [IDSession sharedInstance].userid
NSString * userid = #"1";
}
return session;
}
#end
in retrieving class.
#import "session.h"
NSString *userid =[Session sharedInstance].userid;
NSLog(#"userid retrieved from session variable is %#",userid);
The value that appears in log is (null)
Thanks in advance for any suggestions.
You are setting a local userid variable, not the property of the singleton.
Rather than:
NSString *userid = #"...";
You want:
session.userid = #"...";
Let us say I have an NSObject Class Person.
#interface Person : NSObject
#property NSString *id;
#property NSString *name;
#property Address *billingAddress;
#end
#interface Address : NSObject
#property NSString *lane;
#property NSString *country;
#property NSString *zip;
#end
Now when I fetch the response from a URL, the response is in the form:
{
"response":
{
"Persons":[{"id":"2232","name":"John","Address":{"lane":"Adelaide Avenue","country":"Canada","zip":"45503"}}{"id":"3422","name":"Jane","Address":{"lane":"Victoria Avenue","country":"Australia","zip":"34903"}}]
}
}
I want to parse the response directly into objects without having to write a method to read and assign objects from NSDictionary. Is there are no objects to parse directly from the response to Object based on the Object parameters similar to "GSon" in Android.
EDIT:
I have used the below code to have generic class that does the job for strings without having to know about the object itself.
for (NSString *key in [dct allKeys]) {
if ([cat respondsToSelector:NSSelectorFromString(key)]) {
[cat setValue:[dct valueForKey:key] forKey:key];
}
}
There is no such magic, not even in Android's GSon!!!
Some where down the line you need to write code for converting JSON to your object.
You may create a generic class, or a method (just once) to convert all dictionary values to your object.
After some digging I did get a JSON framework that does exactly what I wanted - JSONModel.
We just need to specify Models and relationships and all the logic for converting JSON response to the models is handled by the framework. Very handy.
Basic usage :
Consider you have a JSON response like
{"id":"10", "country":"Germany", "dialCode": 49, "isInEurope":true}
The corresponding model will be
#import "JSONModel.h"
#interface CountryModel : JSONMode
#property (assign, nonatomic) int id;
#property (strong, nonatomic) NSString* country;
#property (strong, nonatomic) NSString* dialCode;
#property (assign, nonatomic) BOOL isInEurope;
#end
We don't need to write additional code in the .m file to parse and assign values to the variables. Now to initialise the model from the response we just need to do the below
NSString* json = (fetch JSON here)
NSError* err = nil;
CountryModel* country = [[CountryModel alloc] initWithString:json error:&err];
The works well with complex data structures as well.
This question already has answers here:
objects conforming to nscoding will not writetofile
(2 answers)
Closed 8 years ago.
I would like to have an array of model and a field of this model is a mutable array.
Let's say, for example ,in my Model.h I have :
#interface myModel : NSObject
#property (assign,nonatomic) NSString* name;
#property (assign,nonatomic) NSString* time;
#property (assign,nonatomic) NSMutableArray * songs;
#end
then my view controller I have:
NSMutableArray * Storage;
myModel * arr;
arr =[[alarmModel alloc] init];
arr.name=#"pippo";
arr.time=#"01:00:00";
arr.songs=[NSMutableArray arrayWithObjects:#"pippo",#"pluto",#"paperino", nil];
[storage addObject:arr];
[storage writeToFile:filePath atomically:YES]
the last command return "NO" it means that write to file failed.
Is it possible to do what I want to do?
you should init your NSMutableArray *storage, try:
NSMutableArray *storage = [[NSMutableArray alloc] init];
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I need to access userEMail (NSString) property in Class2.m but its returning null.
Class1.h:
#interface AuthController : UIViewController
{
#public NSString *const userPassword;
#public NSString *const userEMail;
#public NSString *const userFullName;
}
#property (nonatomic, strong) NSString *userEMail;
Then in Class1.m im saving userEMail.
Class2.m:
AuthController *ac = [[AuthController alloc] init];
NSLog(#"%#", ac.userEMail);
I just put basic step here, change it as per your requirement.
You just need to write
#property (nonatomic, strong) NSString *userEMail;
in your first.h file
write second.h file
#property (nonatomic, strong) NSString *gotUserEMail;
And your first.m file
you have gotUserEMail string with some value..
pass it to anotherViewController such liek,
secondViewController *addView = [[secondViewController alloc] init];
addView.gotUserEMail = userEMail;
.
.
.
.
EDITE
Okay then you need to use NSUserDefault.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setValue:userEMail forKey:#"myEmailString"];
[userDefaults synchronize];
get anywhere in your project by,
NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
NSString *gotEmail = [data floatForKey:#"myEmailString"];
After reading a comment on another answer
I need to get userEMail value in multiple classes, so its wrong to send data to every class
It sounds more like you are after a singleton class. So try something like this
AuthObject.h
#interface AuthObject : NSObject
#property (nonatomic, strong) NSString *userEMail;
+ (AuthController*)authInstance;
#end
AuthObject.m
#import "AuthObject.h"
#implementation AuthObject
#synthesize userEMail = _userEMail;
static AuthObject *authObjectSharedInstance = nil;
+ (AuthObject *)authInstance
{
static dispatch_once_t instanceToken;
dispatch_once(&instanceToken, ^{
authObjectSharedInstance = [[AuthObject alloc] init];
});
return authObjectSharedInstance;
}
#end
then in another method in another class as long as you have imported AuthObject.h somewhere you can do
- (void)someMethodIMadeUpInClass1
{
AuthObject *authObj = [AuthObject authInstance];
[authObj setUserEMail:#"myemail#address.com"];
}
then in a completely different class you can do
- (void)someMethodIMadeUpInClass2
{
AuthObject *authObj = [AuthObject authInstance];
NSLog(#"my email : %#", [authObj userEMail];
}
In theory if you're creating a singleton class and not going through a sharedInstance (authInstance here) then that code is broken. Attempting to hide that brokenness is just going to cause pain later on. This is why I would chose a singleton class over using NSUserDefaults.