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.
Related
I create the custom class name with FileModel.
FileModel.h
#import <Foundation/Foundation.h>
#interface FileModel : NSObject
#property (nonatomic, copy) NSString *fileName;
#property (nonatomic, copy) NSString *fileType;
#property (nonatomic, strong) NSDate *editDate;
#property (nonatomic, assign) NSInteger fileSize;
#end
I want to compare the particular string with the fileName.
I create the sample like below .m
- (void)viewDidLoad {
[super viewDidLoad];
NSArray *fileSampleName = [[NSArray alloc] initWithObjects:#"apple.png",#"banana.png",#"cherry.png",#"durian.png",#"grape.png",#"avocado.png", nil];
NSMutableArray *fileData = [NSMutableArray new];
FileModel *fileModel = nil;
for( NSInteger i = 0 ; i < fileSampleName.count ; i++){
fileModel = [FileModel new];
fileModel.fileName = [fileSampleName objectAtIndex:i];
fileModel.fileType = #"photo";
fileModel.fileSize = 0;
fileModel.editDate = [NSDate new];
[fileData addObject:fileModel];
}
// fileData's fileName containsObject #"grape" or not?
}
NSArray has containsObject method.
But How can I check the #"grape" is containsObject using fileData at the custom class property filename?
I known using for loop compare one by one.
Did they have other method to check like containsObject?
--- edit---
I try to using indexOfObjectPassingTest method , But the result always is 1.
BOOL result = [fileData indexOfObjectPassingTest:^ BOOL (id tr,NSUInteger index, BOOL *te){
FileModel *fileModel = (FileModel*)tr;
if([#"orange" isEqualToString: fileModel.fileName]){
*te = YES;
return YES;
}else{
return NO;
}}];
NSLog(#"result:%#",#(result)); // it always return 1
Why? thank you very much.
Take a look that NSArray class reference in Xcode. One method you could use is indexOfObjectPassingTest. There are number of related methods depending on your needs. All take a block that's used to test objects to see if they meet whatever criteria you want. In your case you'd test the fileName string.
So you'd pass in a closure that compared the fileName property of each object to your desired filename.
I was stuck on writing NSDictionary into Object process, I am sure that problem is simple as I imagine but would be great to get assistant. Here is my code:
my custom object:
#interface User : NSObject
#property (nonatomic, retain) NSString *cId;
#property (nonatomic, retain) NSString *firstName;
#property (nonatomic, retain) NSString *lastName;
....
-(instancetype) initWithParameters:(NSDictionary*) parameters;
#end
#import "User.h"
#implementation User
-(instancetype) initWithParameters:(NSDictionary*) parameters
{
self = [super init];
if (self) {
[self setParameters:parameters];
}
return self;
}
- (void) setParameters:(NSDictionary*) parameters{
_cId = parameters[#"cId"];
_firstName = parameters[#"first_name"];
_lastName = parameters[#"last_name"];
....
}
and writing process:
id userObjects = [resultData objectForKey:#"data"];
NSMutableArray* mUsers = [[NSMutableArray alloc] init];
for (NSDictionary* userParameters in userObjects) {
User *user = [[User alloc] initWithParameters:userParameters];
[mUsers addObject:user];
}
userObjects - NSArray got from JSON object from server data.
The problem is : nothing happening and user object still empty after initialization, then I have tried - setValuesForKeysWithDictionary after I called variables same as keys in dictionary and nothing changed.
after adding in mUsers:
Could anybody tell me what I am doing wrong? Thank you!
I believe you think those objects are uninitialized because you are seeing 0 key/value pairs next to each User object.
Your code looks good and I think things will change once you implement [NSObject description] (or [NSObject debugDescription]) like this:
- (NSString *)description
{
return [NSString stringWithFormat:#"cId=%#, firstName=%#, lastName=%#",
_cId, _firstName, _lastName];
}
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 = #"...";
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];
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.