I have this working in another app just fine, exact same code and everything is linked in storyboard? I have no Idea what is going on. It seems as the assignmentInfo.className string is kept to nul. and the description method is also nul. Take a look:
AddEditViewController.h -
{
IBOutlet UIDatePicker *dateTimePicker;
}
#property (nonatomic, strong) IBOutlet UITextField *className;
#property (nonatomic, strong) IBOutlet UITextField *assignmentTitle;
#property (nonatomic, strong) IBOutlet UITextField *assignmentDescription;
#property (nonatomic, strong) IBOutlet UISwitch *procrastinationNotificationSwitch;
#property (nonatomic,strong)AssignmentInfo *assignmentInfo;
- (IBAction)addTheInfo:(id)sender;
AddEditViewController.m -
{
if (!_assignmentInfo) {
_assignmentInfo = [[AssignmentInfo alloc]init];
}
return _assignmentInfo;
}
- (IBAction)addTheInfo:(id)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date];
self.assignmentInfo.className = self.className.text;
self.assignmentInfo.assignmentTitle = self.assignmentTitle.text;
self.assignmentInfo.assignmentDescription = self.assignmentDescription.text;
self.assignmentInfo.dateTimeString = dateTimeString;
NSLog(#"%#",self.assignmentInfo.className);
NSLog(#"%#",self.assignmentInfo.description);
[self presentMessage:self.assignmentInfo.description];
}
AssignmentInfo.h -
#property (nonatomic,strong)NSString *className;
#property (nonatomic,strong)NSString *assignmentDescription;
#property (nonatomic,strong)NSString *assignmentTitle;
#property (nonatomic,strong)NSString *dateTimeString;
#property (nonatomic)bool notifcationStatus;
AssignmentInfo.m -
-(NSString *)description
{
return [NSString stringWithFormat:#"Class: %#\r Assignment Title: %# \rAssignment Description: %# \rDue: %# \r%s", self.className, self.assignmentTitle, self.assignmentDescription, self.dateTimeString,self.notifcationStatus ? "Notification On" : "Notification Off"];
}
I had it incorrect before :
-(AssignmentInfo *)classObject
{
if (!_assignmentInfo) {
_assignmentInfo = [[AssignmentInfo alloc]init];
}
return _assignmentInfo;
}
After:
-(AssignmentInfo *)assignmentInfo
{
if (!_assignmentInfo) {
_assignmentInfo = [[AssignmentInfo alloc]init];
}
return _assignmentInfo;
}
Just a silly mistake.
Related
I have a table in SQLite I want to insert data in that table. The table has responses_id, participant_id, answer_text, answer_option_update_date_time. responses_id and participant_id are integers. When I am assigning any thing to participant_id it gives an error, object can not set to property.
#interface Coffee : NSObject {
NSInteger coffeeID;
NSInteger participant_Id;
NSString*question_Id;
NSString*answer_option;
NSString*answer_text;
NSString*update_date_time;
//Intrnal variables to keep track of the state of the object.
}
#property (nonatomic, readonly) NSInteger coffeeID;
#property (nonatomic, retain) NSInteger participant_Id;
#property (nonatomic, copy) NSString *question_Id;
#property (nonatomic, copy) NSString *answer_option;
#property (nonatomic, copy) NSString *answer_text;
#property (nonatomic, copy) NSString *update_date_time;
- (void)save_Local {
CereniaAppDelegate *appDelegate = (CereniaAppDelegate *)[[UIApplication sharedApplication] delegate];
Coffee *coffeeObj = [[Coffee alloc] initWithPrimaryKey:0];
coffeeObj.participant_Id=mynumber;
NSString*question="1";
coffeeObj.question_Id=question;
coffeeObj.answer_option=selectionAnswerOption;
coffeeObj.answer_text=professionTextField.text;
NSDate* date = [NSDate date];
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:#"yyyy-MM-dd HH:MM:SS"];
NSString* str = [formatter stringFromDate:date];
UPDATE_DATE_TIME=str;
coffeeObj.update_date_time=UPDATE_DATE_TIME;
//Add the object
[appDelegate addCoffee:coffeeObj];
}
When I am assigning a value to participant_id it gives an error.
NSInteger is not a class, it is a basic type like an int or long. On iOS NSInteger is typedefed to an int, on OS X it is typedefed to a long. Therefore, you shouldn't try to retain an NSInteger. You should change your property declaration to:
#property (nonatomic, assign) NSInteger participant_Id;
The same goes for your coffeeID property.
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.)
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.
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;
I'm trying to test for memory leaks in my iphone and I'm not having much luck getting rid of this one. Here is the code that is leaking.
- (id)initWithManagedObjectContext:(NSManagedObjectContext *)aMoc delegate:(id)aDelegate runSync:(BOOL)aRunSync {
if (self = [super init]) {
self.moc = aMoc;
self.settingsManager = [[VacaCalcSettingsManager alloc] initWithManagedObjectContext:self.moc];
self.delegate = aDelegate;
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
self.runSync = aRunSync;
}
return self;
}
It is leaking on the self.settingsManager = [[VacaCalcSettingsManager alloc] initWithManagedObjectContext:self.moc]; line.
The self.settingManager instance variable is released in the dealloc method of the class.
I'm not sure what other information would be pertinent. Please let me know and I can provide it.
Thanks for any assistance.
-Mark
Here is the header file.
#interface VacaCalcCalculation : NSObject {
NSManagedObjectContext *moc;
VacaCalcSettingsManager *settingsManager;
id delegate;
NSCalendar *calendar;
NSDate *nextBankLimitDate;
BOOL runSync;
}
#property (nonatomic, retain) NSManagedObjectContext *moc;
#property (nonatomic, retain) VacaCalcSettingsManager *settingsManager;
#property (nonatomic, retain) id delegate;
#property (nonatomic, retain) NSCalendar *calendar;
#property (nonatomic, retain) NSDate *nextBankLimitDate;
#property (nonatomic) BOOL runSync;
- (id)initWithManagedObjectContext:(NSManagedObjectContext *)aMoc delegate:(id)aDelegate;
If your settingsManager property is set to retain then you are retaining an object twice with the line: self.settingsManager = [[VacaCalcSettingsManager alloc] initWithManagedObjectContext:self.moc];
Try adding autorelease to the alloc]init or creating a class method on VacaCalcSettingsManager that returns an autoreleased object. Otherwise you could redeclare your property with assign so that it does not retain the object a second time.