Singleton changes is not preserved to the next view IOS - iphone

I have a singleton class which I intend to share throughout my whole application.
It looks like this:
.m
#implementation PersonalGlobal
#synthesize firstName;
#synthesize lastName;
#synthesize SSN;
#synthesize customerNo;
#synthesize email;
#synthesize address;
#synthesize city;
#synthesize postalCode;
#synthesize telNo;
#synthesize mobileNo;
#pragma mark Singleton Methods
+ (id)sharedPersonal {
static PersonalGlobal *sharedPersonalGlobal = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedPersonalGlobal = [[self alloc] init];
});
return sharedPersonalGlobal;
}
- (void)dealloc {
[super dealloc];
// Should never be called
}
#end
.h
#import <foundation/Foundation.h>
#interface PersonalGlobal : NSObject {
NSString *firstName;
NSString *lastName;
NSString *SSN;
NSString *customerNo;
NSString *email;
NSString *address;
NSString *city;
NSString *postalCode;
NSString *telNo;
NSString *mobileNo;
}
#property (nonatomic, retain) NSString *firstName;
#property (nonatomic, retain) NSString *lastName;
#property (nonatomic, retain) NSString *SSN;
#property (nonatomic, retain) NSString *customerNo;
#property (nonatomic, retain) NSString *email;
#property (nonatomic, retain) NSString *address;
#property (nonatomic, retain) NSString *city;
#property (nonatomic, retain) NSString *postalCode;
#property (nonatomic, retain) NSString *telNo;
#property (nonatomic, retain) NSString *mobileNo;
+ (id)sharedPersonal;
#end
In the code I save strings to it like so:
PersonalGlobal *sharedPersonal = [PersonalGlobal sharedPersonal];
sharedPersonal.firstName = #"Some string";
But when I change view and try to access the string like this:
PersonalGlobal *sharedPersonal = [PersonalGlobal sharedPersonal];
//Setting some textfield
sometextfield.text = sharedPersonal.firstName;
I get nothing. I have done a #import "PersonalGlobal.h" in all the files.
Can i commit the changes in any way to the singleton class?
Can anyone see what I am doing wrong here?
Thanks!

Your implementation is looking fine. This should work if your singleton returning the only one instance . So the point of doubt in your sharedPersonal method . Just try to add breakpoints in this method and see whether it is creating a new instance every time .for the reference I got this SO question.
If not then you can also try this :
+(SingleTon *)getSharedInstance
{
static PersonalGlobal *sharedPersonalGlobal = nil;
if (sharedPersonalGlobal==nil)
{
sharedPersonalGlobal=[[PersonalGlobal alloc]init];
}
return sharedPersonalGlobal;
}
This is working code for me.

Your singleton implementation looks ok. I wouldn't be surprised if in this case the code that you wrote to set the firstName just never gets executed. If I were you I would step through the codes.

Related

using an NSArray in a custom class isn't working

Starting over. I am fairly new to objective C. I have created the following class and I can't figure out how to initialize the array.
Can anyone provide any guidance on how to initialize the NSArray?
StatusPost.m
#import "StatusPost.h"
#implementation StatusPost
#synthesize messageId, fromName, friendId, message, choice2, choice3, choice4, picture, fbImage, commentCount, commentArray;
-(id)initWithMessageId:(NSString*) rMessageId
fromName:(NSString*) rFromName
friendId:(NSString*) rFriendId
message:(NSString*) rMessage
choice2:(NSString*) rChoice2
choice3:(NSString*) rChoice3
choice4:(NSString*) rChoice4
picture:(NSString *) rPicture
fbImage:(UIImage *)rfbImage
commentCount:(NSString*) rCommentCount
commentArray:(NSArray*) rCommentArray
{
if (self = [super init]) {
commentArray = [NSArray new];
self.messageId = rMessageId;
self.fromName = rFromName;
self.friendId = rFriendId;
self.message = rMessage;
self.choice2 = rChoice2;
self.choice3 = rChoice3;
self.choice4 = rChoice4;
self.picture = rPicture;
self.fbImage = rfbImage;
self.commentCount = rCommentCount;
self.commentArray = rCommentArray;
}
return self;
}
- (void)dealloc {
[messageId release];
[fromName release];
[friendId release];
[message release];
[picture release];
[fbImage release];
[commentCount release];
[commentArray release];
[super dealloc];
}
#end
StatusPost.h:
#import
#interface StatusPost : NSObject {
NSString* messageId;
NSString* fromName;
NSString* friendId;
NSString* message;
NSString* choice2;
NSString* choice3;
NSString* choice4;
NSString* picture;
UIImage* fbImage;
NSString* commentCount;
NSArray* commentArray;
}
#property (nonatomic, retain) NSString* messageId;
#property (nonatomic, retain) NSString* fromName;
#property (nonatomic, retain) NSString* friendId;
#property (nonatomic, retain) NSString* message;
#property (nonatomic, retain) NSString* choice2;
#property (nonatomic, retain) NSString* choice3;
#property (nonatomic, retain) NSString* choice4;
#property (nonatomic, retain) NSString* picture;
#property (nonatomic, retain) UIImage* fbImage;
#property (nonatomic, retain) NSString* commentCount;
#property (nonatomic, retain) NSArray* commentArray;
-(id)initWithMessageId:(NSString*) rMessageId
fromName:(NSString*) rFromName
friendId:(NSString*) rFriendId
message:(NSString*) rMessage
choice2:(NSString*) rChoice2
choice3:(NSString*) rChoice3
choice4:(NSString*) rChoice4
picture:(NSString*) rPicture
fbImage:(UIImage*) rfbImage
commentCount:(NSString*) rCommentCount
commentArray:(NSArray*) rCommentArray;
#end
It is likely that you aren't ever initializing the array, so when you try to add an object, you are just sending a message to nil. In the custom class's init method, add the line:
commentArray = [NSMutableArray new];
[NSArray new] is shorthand for [[NSArray alloc] init], so, technically speaking, that statement "inits" the NSArray.
However, your code looks a bit peculiar. You have the following statements in your init:
commentArray = [NSArray new];
self.commentArray = rCommentArray;
The first statement is setting the instance variable commentArray to the address of the newly alloced/inited NSArray while the second is setting the property commentArray to a parameter value. However, you have (through the #synthesize) made the instance variable commentArray the "backing store" for the property commentArray, so when you execute the second line the effect of the first line is overwritten (and the NSArray you created is "leaked").
(But if your real question is how to "load" an NSArray with values, you should ask that question -- and you'll get different answers.)

I get a "Incorrect decrement of the reference count of an object that is not owned at this point by the caller"

When I do Analyze to find out the potential memory leak, I get a "Incorrect decrement of the reference count of an object that is not owned at this point by the caller" :
- (int)downloadUrlTofolder:(NSString *)url filename:(NSString *)name tryTime:(int)tryTime
{
int result = 0;
GetFtpService *ftpService = [[GetFtpService alloc] initwithUrlandOutPut:url output:name];
//I have delete the code here, but problem is not solved.
[ftpService release]; //the potential problem point to this line
return result;
}
Below is the "initwithUrlandOutPut" method:
- (id)initwithUrlandOutPut:(NSString *)url output:(NSString *)o
{
if(self = [super init]) {
self.urlInput = url;
self.outPath = o;
self.success = [NSString stringWithString:#"success"];
self.connected = nil;
}
return self;
}
And the interface:
#interface GetFtpService : NSObject <NSStreamDelegate>
#property (nonatomic, retain) NSInputStream *networkStream;
#property (nonatomic, copy) NSString *urlInput;
#property (nonatomic, retain) NSInputStream *fileStream;
#property (nonatomic, copy) NSString *outPath;
#property int tryTime;
#property (nonatomic, copy) NSString *success;
#property (nonatomic, copy) NSString *connected;
- (id) initwithUrlandOutPut:(NSString *)url output:(NSString *)o;
I want to know why this happened? and how to fix it?
I suspect it's because the 'w' in "initwith..." is not capitalized. Maybe the analyzer is not recognizing the method as an init method because of that.

Iphone: How to get all attributes of a class?

I have this class:
#interface Mission : BaseModel
{
NSString *Description;
NSDate *EndDate;
NSMutableArray *MissionSectionList;
NSString *Name;
NSDate *StartDate;
}
#property (nonatomic, retain) NSString *Description;
#property (nonatomic, retain) NSDate *EndDate;
#property (nonatomic, retain) NSMutableArray *MissionSectionList;
#property (nonatomic, retain) NSString *Name;
#property (nonatomic, retain) NSDate *StartDate;
#end
Is it possible to find out all the atrributes of this class?
I tried it with
Ivar class_getClassVariable(Class cls, const char* name)
But I don't know what the name should be:
name: The name of the class variable definition to obtain.
And actually I want to get all of them without knowing the name.
Is this possible?
You can use class_copyIvarList function to get the list of all instance variables of the class. Then you can iterate through it to get attributes of each ivar. (Note that you're responsible for freeing memory used for ivars array). Sample code may look like:
unsigned int count;
Ivar *varList = class_copyIvarList([Mission class],&count);
for (int i = 0; i < count; ++i){
Ivar var = varList[i];
// Do when you need with ivar
}
free(varList);

EXC_BAD_ACCESS when accessing UIDatePicker

I want to read the value from my DatePicker and store it in my singleton, but I get an exception when trying to do this.
Here is the singleton interface:
#interface Helper : NSObject {
NSMutableArray *array;
//Information For Filter page
NSMutableArray *towns;
NSMutableArray *types;
//Shared resources to apply filters
NSDate *toDate;
NSDate *fromDate;
NSString *selectedType;
NSString *selectedTown;
//Shared resource of which button was clicked
int clicked;
int clickedCell;
int clickedContext;
}
#property (nonatomic, retain) NSMutableArray *array;
#property (nonatomic, retain) NSMutableArray *towns;
#property (nonatomic, retain) NSMutableArray *types;
#property (nonatomic) int clicked;
#property (nonatomic) int clickedCell;
#property (nonatomic) int clickedContext;
#property (nonatomic, retain) NSDate *toDate;
#property (nonatomic, retain) NSDate *fromDate;
#property (nonatomic, retain) NSString *selectedType;
#property (nonatomic, retain) NSString *selectedTown;
+(id)sharedManager;
#end
This is the function where the exception happens
-(void) viewWillDisappear:(BOOL)animated
{
Helper *myHelper = [Helper sharedManager];
if(myHelper.clickedContext == 0)
{
if(myHelper.clickedCell == 0)
{
//causes exception
myHelper.fromDate = [self.fromDatePicker date];
}
else
{
//causes exception
myHelper.toDate = [self.toDatePicker date];
}
}
else
{
if(myHelper.clickedCell == 0)
{
myHelper.selectedTown = [myHelper.towns objectAtIndex:[self.townPicker selectedRowInComponent:0]];
}
else
{
myHelper.selectedType = [myHelper.types objectAtIndex:[self.typePicker selectedRowInComponent:0]];
}
}
[super viewWillDisappear:animated];
}
declaration of pickers
#property (nonatomic, retain) IBOutlet UIDatePicker *toDatePicker;
#property (nonatomic, retain) IBOutlet UIDatePicker *fromDatePicker;
#property (nonatomic, retain) IBOutlet UIPickerView *typePicker;
#property (nonatomic, retain) IBOutlet UIPickerView *townPicker;
#synthesize part
#synthesize typePicker, toDatePicker, fromDatePicker, townPicker, townsView, toDateView, typesView, fromDateView;
Any idea's why?
Thanks
If your outlet is not assigned in interface builder this can happen.
If you think it is, try running the app with NSZombieEnabled = YES and see if you get any error messages.
I found the problem. In my Helper Class I should have assigned not retained. So it should have been
#property (nonatomic, assign) NSDate *toDate;
#property (nonatomic, assign) NSDate *fromDate;

EXC_BAD_ACCESS when calling class init in Objective C

I've been trying to figure this out but I can't figure out what I'm doing wrong.
I wrote a class and whenever I try to initialize it, I get a EXC_BAD_ACCESS error. I can't even step into the initialization.
Anyone have any idea what I'm doing wrong?
User *myUser = [myUser init];
.h file:
#import <Foundation/Foundation.h>
#interface User : NSObject {
long rowId;
NSString *email;
NSString *password;
NSString *fileVersion;
}
#property long rowId;
#property (assign) NSString *email;
#property (assign) NSString *password;
#property (assign) NSString *fileVersion;
#end
.m file
#import "User.h"
#implementation User
#synthesize rowId, email, password, fileVersion;
-(id)init {
self = [super init];
return self;
}
#end
You have to actually allocate the object:
User *myUser = [[User alloc] init];
Don't forget to release it when you're done using it.