ios/xcode/objective c: set and get session variable [duplicate] - ios

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 = #"...";

Related

checking for nil before sending message

I am having this issue in Xcode 6. It is fine if I use this in Xcode 5 or below.
Objects are automatically instantiated even though it's nil and can't check nil.
NSString *userID = [DataManager sharedInstance].currentUser.userId;
if (!userID) {
userID = #"user";
}
[userInfo setObject:userID forKey:LogDataUserIDKey];
That's what I am doing and I am getting EXE_BAD_ACCESS(code=1, address=0x443b800c) at the last line.
userID is instantiated as my attached screenshot. How can I check whether userID is nil. I am lost. Please advise.
OK. I am able to fix the issue. It is because inside this User object which is [DataManager sharedInstance].currentUser, userId is declared as both instance variable and property.
#interface User : NSObject <NSCopying>{
NSString *userId; // instance variable
}
#property (nonatomic, retain) NSString * userId;
So, by removing the instance variable declaration and it fixed the issue.
#interface User : NSObject <NSCopying>
#property (nonatomic, retain) NSString * userId;
But, I am not sure why. You guys have any idea?

iOS Use of Undeclared Identifier in Class method for creating a new entity

I am trying to implement a simple factory method for creating a new entity for my core data database. In Pet+Create.h:
+ (Pet *)petWithName:(NSString *)name
weight:(NSNumber *)weight
weightIsInKg:(BOOL)yesorno
inManagedObjectContext:(NSManagedObjectContext *)context
In Pet+Create.m:
+ (Pet *)petWithName:(NSString *)name
weight:(NSNumber *)weight
weightIsInKg:(BOOL)yesorno
inManagedObjectContext:(NSManagedObjectContext *)context
{
Pet *newPet = nil;
newPet = [NSEntityDescription insertNewObjectForEntityForName:#"Pet" inManagedObjectContext:context];
newPet.name = name;
newPet.idealWeight = weight;
newPet.weightIsInKg = [NSNumber numberWithBool:weightIsInKg];
return newPet;
}
And finally, in Pet.h: (which is a generated file by xcode, I have not touched it)
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#interface Pet : NSManagedObject
#property (nonatomic, retain) NSNumber * idealWeight;
#property (nonatomic, retain) NSString * name;
#property (nonatomic, retain) NSNumber * weightIsInKg;
#end
My problem is, I am getting a Use of undeclared identifier: weightIsInKg error message on newPet.weightIsInKg = [NSNumber numberWithBool:weightIsInKg];
Why am I getting this error? I have obviously declared weightIsInKg because it is in the method name! Am I missing something simple?
The parameter with which petWithName is called is named yesorno
weightIsInKg:(BOOL)yesorno
then you refer to it as weightIsInKg
newPet.weightIsInKg = [NSNumber numberWithBool:weightIsInKg];
Use instead
newPet.weightIsInKg = [NSNumber numberWithBool:yesorno];
Your method argument is called yesorno, not weightIsInKg. Use yesorno or sanitize your variable names.

Accessing NSString from another .m file [closed]

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.

Sending a class through json post [duplicate]

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.

Initialization of custom object return null [duplicate]

This question already has an answer here:
why is my code outputting *nil description*
(1 answer)
Closed 10 years ago.
Here is my custom class:
ClassA.h
#interface ClassA : NSObject<RKRequestDelegate>{
NSString *uri;
NSString *folderUri;
NSInteger idFolder;
NSString *kind;
bool isMine;
CustomUser *owner;
NSMutableArray *usersAdministrators;
NSMutableArray *usersContributors;
NSMutableArray *usersReaders;
NSString *visibility;
NSString *name;
NSString *description;
NSMutableArray *comments;
}
#property (nonatomic,copy) NSString *uri;
#property (nonatomic,copy) NSString *folderUri;
#property (nonatomic,assign) NSInteger idFolder;
#property (nonatomic,copy) NSString *kind;
#property (nonatomic,assign) bool isMine;
#property (retain) DMIUser *owner;
#property (nonatomic,copy) NSMutableArray *usersAdministrators;
#property (nonatomic,copy) NSMutableArray *usersContributors;
#property (nonatomic,copy) NSMutableArray *usersReaders;
#property (nonatomic,copy) NSString *visibility;
#property (nonatomic,copy) NSString *name;
#property (nonatomic,copy) NSString *description;
#property (nonatomic,copy) NSMutableArray *comments;
#end
ClassA.m
#implementation ClassA
#synthesize uri,folderUri,idFolder,kind,isMine,owner,usersAdministrators,usersContributors,usersReaders,visibility,name,description,comments;
-(NSString*)description {
return #"ClassA";
}
#end
Quite simple. But when i try to create new instance of this, like this:
datas = [NSMutableArray array]; // Tried with [[NSMutableArray alloc] init] => same thing
ClassA *classA = [[ClassA alloc] init];
[datas addObject:classA];
NSLog(#"classA = %#",classA);
NSLog(#"datas = %#",datas);
First NSLog returns "ClassA".
Second NSLog returns "datas = ()"
What's wrong here? I always created class like this and i've never had problem like this.
Thanks!
Ok guyzzz i found the problem. It's my attribute:
NSString *description;
Seems that iOs doesn't love that. It conflict with the -description method in NSObject...
After that, i found a similar question here:
Why can't I use "description" as an attribute name for a Core Data entity?
Cheers
if your want to return some value implement method description in the ClassA
- (NSString *)description {
return #"ClassA";
}
You've got everything you need. Are you sure the variable you're assigning to isn't a weak one? All delegate properties are weak, therefore not retaining the object. My guess is that you're doing something like this
someObject.delegate = [[ClassA alloc] init];
NSLog(#"%#", someObject.delegate);
Because the delegate property is weak it doesn't hold onto the variable.
Edit:
This whole answer assumes you are using ARC. If not, disregard.

Resources