objective-c: Extremely Strange and annoying string problem - objective-c

i've been working on a tableview program and i have a function that processes several data from user preferences, and core-data. the program parses these things and returns a url adress. heres the code:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *serverAdress = [prefs stringForKey:#"serverAdress"];
serverAdress = [serverAdress stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"dd.MM.yyyy"];
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setDateFormat:#"dd.MM.yyyy HH:mm"];
NSString *dateString = [dateFormatter stringFromDate:[NSDate date]];
NSString *dateTimeString = [NSString stringWithFormat:#"%# %#",dateString,[dashboardParameters attribute1]];
NSTimeInterval dayInterval = [[dashboardParameters attribute2] intValue]*60*60*(-1);
NSDate *date2 = [[dateFormatter2 dateFromString:dateTimeString] addTimeInterval:dayInterval];
NSString *urlString =
[NSMutableString stringWithFormat:#"%#/webservices/service1.asmx/getHourlySales2?tarih2=%#&tarih1=%#&salesType=%#",
serverAdress,
dateTimeString,
[dateFormatter2 stringFromDate:date2],
[dashboardParameters itemOrder]
];
urlString = [urlString stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
return urlString;
Everything works great, i have no problem on retrieving data. But when i execute the code, i get this log output:
//server.dyndns.org/webservices/service1.asmx/getHourlySales2?tarih2=20.01.2011%2016:00&tarih1=19.01.2011%2016:00&salesType=Hepsi/webservices/service1.asmx/getHourlySales2?tarih2=21.01.2011%2022:00&tarih1=21.01.2011%2011:00&salesType=Hepsi
it has to be
//server.dyndns.org/webservices/service1.asmx/getHourlySales2?tarih2=20.01.2011%2016:00&tarih1=19.01.2011%2016:00&salesType=Hepsi
But strangely the program adds
/webservices/service1.asmx/getHourlySales2?tarih2=21.01.2011%2022:00&tarih1=21.01.2011%2011:00&salesType=Hepsi
by itself after the normal processing.
Also another important thing, the code runs normally when i remove
NSString *serverAdress = [prefs stringForKey:#"serverAdress"] from the code and enter serveradress manually.
Plase help , everything in the program works fine but im stuck with this problem.
Thanks for helping.

So what's in serverAdress before you add it to urlString. Looks as if serverAdress contains '//server.dyndns.org/webservices/service1.asmx/getHourlySales2?tarih2=20.01.2011%2016:00&tarih1=19.01.2011%2016:00&salesType=Hepsi'

Related

Changing The Date Values of NSUserDefaults standardUserDefaults from a Dictionary Response

My method receives a Dictionary named responseDict which contains keys, such as id, status, name, phoneNumber, date etc. in
responseDict[#"data"][#"value"][n]
(n for number)
If our code uses NSUserDefaults to access data:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
How can I change the date MySQL attribute 2017-05-12 10:00:00 to Dec 5, 2017 10 AM?
I tried the following, but it does not seem to change the defaults in the UILabel.
[defaults setInteger:[dictionary[#"id"] integerValue] forKey:#"ID"];
[defaults setObject:dictionary[#"status"] forKey:kNSUDoctorStatus];
[defaults setInteger:[dictionary[#"dur"] integerValue] forKey:#"duration"];
[defaults setObject:[NSString stringWithFormat:#"%# some",dictionary[#"date"]] forKey:#"date"];
[defaults synchronize];
Edit:
UILabel
The UILabel seems to have been added to the view by:
UILabel *vb = (UILabel *)[self.view viewWithTag:personMessageViewTag];
which was defined by:
#define personMessageViewTag 3001
Doctor Class
There is also a PersonDetailView class initialized:
PersonDetailView *personOnTheView;
And called in a method:
personOnTheView = [PersonDetailView sharedInstance];
personOnTheView.delegate = self;
[personOnTheView updateValues];
[self.view addSubview:personOnTheView];
Method:
Here is the code for [personOnTheView updateValues]
-(void)updateValues{
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSString *strImageUrl = [NSString stringWithFormat:#"%#%#",baseUrlForXXHDPIImage,[ud objectForKey:kOnGoingBookingPersonProfileImageKey]];
_doctorName.text = [ud objectForKey:kOnGoingBookingPersonNameKey];
NSString *at = [NSString stringWithFormat:#"%#",flStrForObj([Helper getLocalDate:[ud objectForKey:kOnGoingBookingPersonBookingDate]])];
_appoinmentTime.text = at;
float rating = [[ud objectForKey:kOnGoingBookingPersonRatingKey] floatValue];
_starRatingView.value = rating;
[_profilepic sd_setImageWithURL:[NSURL URLWithString:[Helper removeWhiteSpaceFromURL:strImageUrl]]
placeholderImage:[UIImage imageNamed:#"doctor_image_thumbnail"]
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL){
}];
}
Problem:
Everytime I change or modify:
[defaults setObject:dictionary[kPNPayloadAppoinmentTimeKey] forKey:kOnGoingBookingPersonBookingDate];
To:
[defaults setObject:[NSString stringWithFormat:#"%# some",dictionary[#"date"]] forKey:#"date"];
or just as simple as:
[defaults setObject:#"Something here" forKey:#"date"];
It displays blank.
Additional Information Edit:
[Helper getLocalDate] code:
+(NSString *)getLocalDate:(NSString *)gmtdate {
NSString *dateStr = gmtdate;
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
[dateFormatter1 setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
NSDate *destinationDate = [dateFormatter1 dateFromString:dateStr];
NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:#"MMM d, yyyy HH a"];
dateStr = [dateFormatters stringFromDate: destinationDate];
// NSLog(#"DateString : %#", dateStr);
return dateStr;
}
With the help of this method you can get the date in local format if you having time zone in date string
-(NSDate*)convertDateToLocal:(NSString *)strDate
{
NSDateFormatter *localFormat = [[NSDateFormatter alloc] init];
[localFormat setDateFormat:#"yyyy-MM-dd HH:mm:ss z"];
NSDate *localDate = [localFormat dateFromString:strDate];
return localDate;
}
Here are two public method for date conversion if you don't have time zone
1) Date from string,
+ (NSDate *)dateFromString:(NSString *)strSource inFormat:(NSString *)strTargetFormat
{
NSDateFormatter *dtfFormatter = [[NSDateFormatter alloc] init];
[dtfFormatter setDateFormat:strTargetFormat];
return [dtfFormatter dateFromString:strSource];
}
2) String from date,
+ (NSString *)stringFromDate:(NSDate *)dtSourceDate inFormat:(NSString *)strTargetFormat
{
NSDateFormatter *dtfFormatter = [[NSDateFormatter alloc] init];
[dtfFormatter setDateFormat:strTargetFormat];
if (dtSourceDate != nil)
return [dtfFormatter stringFromDate:dtSourceDate];
else
return #"";
}
Hope this helps.

AvAudioRecorder on cancel replace old Audio files

I am using AvAudio Recorder for record Audios in IOS app and save in document directory but when i record a audio file and then again going for new recording and during recording click on cancel button (manually added) the previous file is replacing please suggest any answer
Thanks in advance
- (NSString *) dateString
{
// return a formatted string for a file name
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"ddMMMYY_hhmmssa";
return [[formatter stringFromDate:[NSDate date]] stringByAppendingString:#".aif"];
}
save with date and time
like this
NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath_ = [searchPaths objectAtIndex: 0];
NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:[self dateString]];
// File URL
NSURL *url = [NSURL fileURLWithPath:pathToSave];//FILEPATH];

How to convert Date from JSON to NSDate

Hi I need to convert this string
"2013-12-05T08:58:55.9345456+02:00"
to NSDate
I am trying next format but without luck
"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"
Code below.
Can you help me with right format
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
// "2013-12-05T08:58:55.9345456+02:00"
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:SSSSSSSZZZZZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
Your format is close. You need:
yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZZZZZ
And be sure to set the formatter's locale to en_US_POSIX. This should always be done when parsing fixed formatted, non-localized strings.
NSString * dateStr = #"2013-12-05T08:58:55.9345456+02:00";
NSArray *dateStrParts = [dateStr componentsSeparatedByString:#"T"];
NSString *datePart = [dateStrParts objectAtIndex:0];
NSString *timePart = [dateStrParts objectAtIndex:1];
NSString *t = [[timePart componentsSeparatedByString:#"."] objectAtIndex:0];
NSString *newDateStr = [NSString stringWithFormat:#"%# %#",datePart,t];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[df setDateFormat:#"yyyy-MM-dd HH:mm:ss"]; // Change here for your formated output
NSDate *date = [df dateFromString:newDateStr];
NSLog(#"%#",date);
Output
2013-12-05 08:58:55 +0000
The problem lies in the timezone where you have +02:00.
The ZZZZ only parses to +0000, hence you are getting nil.
You can do in this way:
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSString *jsonDateString = #"2013-12-05T08:58:55.9345456+02:00";//#"2013-12-05T08:58:55.9345456+02:00"
NSRange lastColon = [jsonDateString rangeOfString:#":" options:NSBackwardsSearch];
if(lastColon.location != NSNotFound) {
jsonDateString = [jsonDateString stringByReplacingCharactersInRange:lastColon withString: #""];
}
[dateFormat setDateFormat:#"yyyy-MM-dd'T'HH:mm:ss'.'SZ"];
NSDate *date = [dateFormat dateFromString:jsonDateString];
NSLog(#"%#",date)
EDIT:
As per rmaddy's comment, you should use SSSSS instead of single S.

NSDate from NSString read from sqlite database

I have the need of getting NSDate format from a NSString *stringDate and I would do that by using the following code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss";
NSDate *date = [formatter dateFromString:stringDate];
The above code works fine if I initialize my string manually NSString *stringDate = #"2013-01-14 17:55:53";, so I get the date in NSDate format correct.
But I am reading the string from an sqlite3 database, so the string is initialized as follows:
NSString *stringDate = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
The string is NSLogged correctly, but when I try to get the NSDate format i get only (null).
Any ideas of what could be the problem? Thanks!
The string you read from the sqlite is not in the format that you are using in dateformatter.
EDIT:
Use formatter.dateFormat = #"yyyy-MM-dd HH:mm:ss z"; or trimming time zone (+0000) will solve your problem.-By Inder Kr. Rathore

How to make a string from a NSDate variable as well as another NSString variable

I am playing around with making a unique ID for whenever a button is pushed in my application. I basically want it to be two NSString variables that I make concatenated together.
So far, what I have got it mostly working as it gives me no errors in the code itself, but it does only give me (null) as my text in my label.
Where I have the 2 is basically a placeholder for where I will eventually have a variable that will look up what is in the plist for the username.
My code for this is:
NSString *UserID;
NSDate *TimeNow;
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMddhhmm"];
UserID = [NSString stringWithFormat:#"",'Username'];
NSString *CurrentTime = [dateFormatter stringFromDate:TimeNow];
SessionID = [NSString stringWithFormat:#"%# %#", UserID, CurrentTime];
UniqueSessionID.text = SessionID;
You haven't initialised TimeNow. And your UserID is just being set to the empty string. To clean up that code, I would do this:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMddhhmm"];
NSString *CurrentTime = [dateFormatter stringFromDate:[NSDate date]];
NSString *SessionID = [NSString stringWithFormat:#"Username %#", CurrentTime];
UniqueSessionID.text = SessionID;
Update:
To have "Username" dynamic just do this:
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyyMMddhhmm"];
NSString *CurrentTime = [dateFormatter stringFromDate:[NSDate date]];
NSString *UserID = #"Some user";
NSString *SessionID = [NSString stringWithFormat:#"%# %#", UserID, CurrentTime];
UniqueSessionID.text = SessionID;
Then just change UserID to what you want the username to be.
I think that there should be:
NSDate *TimeNow = [NSDate date];
NSDate *dateFromString = [[NSDate alloc] init];
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy/MM/dd hh:mm"];//this is the date format of dateFromString
NSString *CurrentTime = [dateFormatter stringFromDate:TimeNow];
[dateFromString release];
UserID = [NSString stringWithFormat:#"",'Username'];
SessionID = [NSString stringWithFormat:#"%# %#", UserID, CurrentTime];
UniqueSessionID.text = SessionID;