Property not found on object of type error - objective-c

I am trying to access a class method from another class but I am getting error. I have a class 'Constants' in which I have written a class method 'changeDateFormat' and accessing it from some viewController.
Here is my code.
Constants.h
#interface Constants : NSObject
+(NSString *)changeDateFormat:(NSString *)currentDate;
#end
Constans.m
#implementation Constants
+(NSString *)changeDateFormat: (NSString *) currentDate
{
NSString *convertedDate = #"";
#try
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM dd yyyy"];
NSDate *date = [dateFormatter dateFromString:currentDate];
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc]init];
[dateFormatter1 setDateFormat:#"yyyy-mm-dd"];
convertedDate = [dateFormatter1 stringFromDate:date];
}
#catch(NSException *e)
{
NSLog(#"%# %#", e.name, e.reason);
}
return convertedDate;
}
#end
I am trying to access the above method like Constants.changeDateFormat:#"Mar 11 2018" but I am getting error - Property not found on object of type.

You are sending the changeDateFormat: method. You don't use a . for that. This is the correct syntax:
NSString *output = [Constants changeDateFormat:input];

Related

Working with Date and Time separately using NSDatePickerView on Xcode

I have a part of app that set date and time of an appointment. For this, I have two NSDatePickerView. The first one I set to NSDatePickerModeDate, while the other one to NSDatePickerModeTime. But actually they should be referring to a same NSDate object inside a NSMutableDictionary entry. I know about NSDatePickerModeDateTime, but I need the date and time to be picked separatedly.
I know how to set up the NSDatePickerView to show and hide and event control and such, but at the event control UIControlEventValueChanged fire for NSDatePickerView, I'm confused on how to code the change for this, and also how to initialise the pickers (datePicker.date = today, timePicker.date = "9:00 AM")
#interface MyViewController () {
NSMutableDictionary *data;
}
#end
#implementation MyViewController
#synthesize datePicker, timePicker;
- (void) viewDidLoad {
[super viewDidLoad];
data = [[NSMutableDictionary alloc] init];
[data setObject:[NSDate date] forKey:#"date"];
datePicker.datePickerMode = UIDatePickerModeDate;
timePicker.datePickerMode = UIDatePickerModeTime;
[datePicker addTarget:self action:#selector(changeDate:) forControlEvents:UIControlEventValueChanged];
[timePicker addTarget:self action:#selector(changeTime:) forControlEvents:UIControlEventValueChanged];
datePicker.date = data[#"date"]; //????
timePicker.date = data[#"date"]; //????
}
- (IBAction) changeDate:(id)sender {
UIDatePickerView *dp = (UIDatePickerView *)sender;
[data setObject:dp.date forKey:#"date"]; //????
}
- (IBAction) changeTime:(id)sender {
UIDatePickerView *tp = (UIDatePickerView *)sender;
[data setObject:tp.date forKey:#"date"]; //????
}
The part that I don't know how to code it is denoted by //????. I've read about NSDateFormatter, NSCalendar, and some kind of date components on some answers, but that was actually making me more confused as it also throws strings and structs into the mix, what to use to do what and when. Please help.
You can set both date pickers to the same date and time. The unused part is there but it isn't displayed and can't be changed. When the user changes the value of one date picker you have to set the other date picker to the same value.
- (IBAction)changeDate:(id)sender {
NSDatePicker *dp = (NSDatePicker *)sender;
[data setObject:dp.dateValue forKey:#"date"];
self.timePicker.dateValue = dp.dateValue;
}
- (IBAction)changeTime:(id)sender {
NSDatePicker *tp = (NSDatePicker *)sender;
[data setObject:tp.dateValue forKey:#"date"];
self.datePicker.dateValue = tp.dateValue;
}
u can try this
///Convert Full date to only date
- (IBAction) changeDate:(UIDatePickerView *)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
NSString *dateStr = [dateFormatter stringFromDate:[sender date]];
[data setObject:dateStr forKey:#"date"];
}
///convert Date to only time format
- (IBAction) changeTime:(UIDatePickerView *)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"hh:mm a"];
NSString *dateStr = [dateFormatter stringFromDate:[sender date]];
[data setObject:dateStr forKey:#"Time"];
}

Storing date value into Core Data in Objective C

Can anyone tell me please how to save only date value to Core Data using objective C? I am using UIDatePicker. I searched a lot, but couldn't find any. Can you be more specific because i am newbie. Thanks
Here is my code for adding data to core data database. But instead of formatting dates into string I would like to save as a NSdate. I am creating an calendar app that stores events into CoreData. Is there any help?
- (IBAction)btnSave:(UIBarButtonItem *)sender {
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *context = [delegate managedObjectContext];
NSManagedObject *dataRecord = [NSEntityDescription insertNewObjectForEntityForName:#"Scheduler" inManagedObjectContext:context];
[dataRecord setValue:self.txtBxCustomerName.text forKey:#"customerName"];
[dataRecord setValue: [self date] forKey:#"date"];
[dataRecord setValue:[self begin] forKey:#"startTime"];
[dataRecord setValue:[self end] forKey:#"endTime"];
NSError *error;
if (![context save:&error]) {
NSLog(#"Error! %#", error);
}
[self dismissViewControllerAnimated:true completion:nil];
}
- (IBAction)datePicker:(id)sender {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"MM-dd-yyyy"];
NSString *string = [dateFormat stringFromDate:self.dateTime.date];
date = string;
}
- (IBAction)startTimePicker:(id)sender {
NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
[timeFormat setDateFormat:#"HH:mm"];
NSString *str = [timeFormat stringFromDate:self.startTime.date];
begin = str;
}
- (IBAction)endTimePicker:(id)sender {
NSDateFormatter *endTimeFormatt = [[NSDateFormatter alloc] init];
[endTimeFormatt setDateFormat:#"HH:mm"];
NSString *sting = [endTimeFormatt stringFromDate:self.endTime.date];
end = sting;
}
Get the date from the picker with
NSDate *chosenDate = myDatePicker.date;
and then set it to the desired attributed (in the example startDate) of your model, which should be define in Core Data as type Date
myCoreDataObject.startDate = chosenDate;
or even shorter
myCoreDataObject.startDate = myDatePicker.date;

"NSDate not an Objective-C object" error shows when run the application in IOS 7 in real device

NSDate object is working for iPhone, iPad when i ran the application in real devices. But when I run the application in iPad real device than it gives <not an Objective-C object> error. I tried to sort out it. but couldn't.
- (NSString*)getDateFromJSONToStringSaveFormat:(NSString *)dateString
{
NSDate *_Date = [NSDate alloc] init];
NSDate *_Date = [self getDateFromJSON:dateString];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
return [dateFormatter stringFromDate:_Date];
}
- (NSDate*) getDateFromJSON:(NSString *)dateString
{
// Expect date in this format "/Date(1268123281843)/"
int startPos = [dateString rangeOfString:#"("].location+1;
int endPos = [dateString rangeOfString:#")"].location;
NSRange range = NSMakeRange(startPos,endPos-startPos);
unsigned long long milliseconds = [[dateString substringWithRange:range] longLongValue];
NSTimeInterval interval = milliseconds/1000;
return [NSDate dateWithTimeIntervalSince1970:interval];
}
because of this issue i initialize the NSDate object and saw the date value. (NSDate *_Date = [NSDate alloc] init];) in here also gives same error? why is that? anyone faced this error ??
First off you can just remove this line:
NSDate *_Date = [NSDate alloc] init];
Since the next line just redeclares it, also you in the line you should remove you are missing a [.
- (NSString*)getDateFromJSONToStringSaveFormat:(NSString *)dateString
{
// Not needed since the line after it also declares the variable.
//NSDate *_Date = [NSDate alloc] init];
NSDate *_Date = [self getDateFromJSON:dateString];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
return [dateFormatter stringFromDate:_Date];
}

NSString to NSDate is NOT working when called multiple times

I have written this function in a class:
- (NSDate *) convertDate : (NSString *) dateStr{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
NSDate *dateFrmStr = [[NSDate alloc] init];
dateFrmStr = [dateFormatter dateFromString:dateStr];
return dateFrmStr;
}
I am calling this function in another class like this:
NSString * dateStr1 =#"01-01-1977";
NSString * dateStr2 =#"22-12-1977";
NSString * dateStr3 =#"19-01-1978";
MyClass *data = [[MyClass alloc]init];
NSDate *dateObj1 = [data convertDate:dateStr1];
NSDate *dateObj2 = [data convertDate:dateStr2];
NSDate *dateObj3 = [data convertDate:dateStr3];
NSLog(#" >>> dateObj1 %#",dateObj1);
NSLog(#" >>> dateObj2 %#",dateObj2);
NSLog(#" >>> dateObj3 %#",dateObj3);
When I run this the only first date seems to get converted because the output I get is :
>>> dateObj1 1977-01-01 00:00:00 +0000
There is no error nothing but the programs just stops.
I've tried you're code and for me works very fine( with ARC ), probably you're issue is somewhere else in "MyClass"…
BTW! Why don't you make a "category" on NSDate?
Something like this :
NSDate + ConvertDate.h
#interface NSDate (ConvertDate)
+(NSDate *) convertDateFromString : (NSString *)dateString;
#end
NSDate + ConvertDate.m
#implementation NSDate (ConvertDate)
+(NSDate *)convertDateFromString:(NSString *)dateString{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd-MM-yyyy"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
return [dateFormatter dateFromString:dateString];
}
#end
Yes it looks like your code, the only differences are that you don't need to allocate an instance of "MyClass" to use the
"convertDateFromString" method because you're using a class method.'
could it be
NSString * dateStr2 =#"22-12-1977";
that NSDate thinks 22 as the month??? maybe try 12-22-1977 instead?? i usually use NSDateFormatter to convert to string or date..

iPhone simple method definition and calling the current date/time

I'm very new to iPhone development, and I'm trying to write a function which will accept one parameter, and return the current date/month and store it in a variable.
But I'm getting a (null) value with NSLog.
Method:
-(NSString *) getNowDateMonth:(NSString *)type {
NSDate *now = [[NSDate alloc] init];
if (type==#"month") {
NSDateFormatter *monthFormat = [[NSDateFormatter alloc] init];
[monthFormat setDateFormat:#"MM"];
NSString *theMonth = [monthFormat stringFromDate:now];
[monthFormat release];
return theMonth;
} else if (type==#"day") {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd"];
NSString *theDate = [dateFormat stringFromDate:now];
//int setDate = theDate;
[dateFormat release];
return theDate;
}
[now release];
return NULL;
}
Calling the function to get value:
NSString *month = [self getNowDateMonth:#"month"];
NSLog(#"%#", month);
Am I going about this the right way?
First of all, compare the strings using [#"month" isEqualToString:type], because two strings containing the same text ("month") may not be equal by the == operator. == checks if they're the same string object, not strings object with the same contents.
Second of all, you're leaking the date when returning the month or day (not releasing now). You should use [NSDate date]; instead of [[NSDate alloc] init].
To sum up, a suggested better version of this method would be:
-(NSString *) getNowDateMonth:(NSString *)type {
NSDate *now = [NSDate date];
if ([#"month" isEqualToString:type]) {
NSDateFormatter *monthFormat = [[NSDateFormatter alloc] init];
[monthFormat setDateFormat:#"MM"];
NSString *theMonth = [monthFormat stringFromDate:now];
[monthFormat release];
return theMonth;
} else if ([#"day" isEqualToString:type]) {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd"];
NSString *theDate = [dateFormat stringFromDate:now];
[dateFormat release];
return theDate;
} else {
return nil;
}
}
Also, there are a few other points that can be taken into consideration to improve this method:
do not use NSString as type; use an enum
do not allocate NSDateFormatter on each call to the method; instead use a static variable in the method
You want to use NSDateComponents to reliably and easily extract unit information i.e. month, day, week etc from an NSDate.
See Date and Time Programming Guide for Cocoa.
Dates are a deceptively complex programing problem so Cocoa has a fully developed set of classes for dealing with them. However, the learning curve is a bit steep.