Simple NSDateFormatter / String Parse - iphone

2012-10-12 19:29:43
Aquivalent NSDateFormatter:
[_dateFormatter setDateFormat:#"yyyy-MM-dd HH:mm:ss"];
Throws an exception ... Why?
Thank you!
Reference: http://waracle.net/mobile/iphone-nsdateformatter-date-formatting-table/
UPDATE:
The problem seems to be the string. If I hardcode the string:
NSString * string = #"2012-10-12 19:29:43";
It works fine.
I read it from an array of key-value pairs so I do:
NSString * string = [NSString stringWithFormat:#"%#", (NSString *)[[NSArray readFromPlistFile:#"latestchangesdates"] valueForKey:#"newsLastEdited"]];
Console Output:
#1:
2012-10-12 10:16:49
#2:
( "2012-10-12 10:16:49" )
I think the problem is something which has to do with the string parse from the array.
UPDATE 2:
[[[NSArray readFromPlistFile:#"latestchangesdates"] objectAtIndex:0] valueForKey:#"newsLastEdited"]]
... finally did it.

It looks like the key newsLastEdited in your Plist file is actually returning an array not a string.
The line:
NSString *string = [NSString stringWithFormat:#"%#", (NSString *)[[NSArray readFromPlistFile:#"latestchangesdates"] valueForKey:#"newsLastEdited"]];
is simply casting to an NSString the returned value from [[NSArray readFromPlistFile:#"latestchangesdates"] valueForKey:#"newsLastEdited"]. This does not automatically make the returned value a string.
What happens if you use the following instead:
NSString *string = [[[NSArray readFromPlistFile:#"latestchangesdates"] valueForKey:#"newsLastEdited"] objectAtIndex:0];
If I'm correct, this will take the first element of the array returned from the Plist newsLastEdited key. If this works then you should probably take some time to understand the data structure stored in the Plist file.

[[[NSArray readFromPlistFile:#"latestchangesdates"] objectAtIndex:0] valueForKey:#"newsLastEdited"]]
... finally did it.

Related

variable is not a CFStringRef

I have this:
partenaire_lat = 48.8160525;
partenaire_lng = 2.3257800;
And obtain a NSString like this:
NSString *endPoint =[NSString stringWithFormat:#"%#,%#", partenaire_lat, partenaire_lng];
and after using this NSString in some context I get this stupid error:
Variable is not a CFString.
But if I create the NSString like this:
endPoint = #"48.8160525,2.3257800" it then works perfect!
For this error Variable is not a CFString I tried the following:
NSString *endPoint1 =[NSString stringWithFormat:#"%#,%#", partenaire_lat, partenaire_lng];
CFStringRef endPoint =(CFStringRef)endPoint1;
and tried to use endPoint but not working neither this way.Anyone any miraculous idea?Thx
EDIT:partenaire_lat and partenaire_lng are both NSString!!
You have
partenaire_lat = 48.8160525;
partenaire_lng = 2.3257800;
You keep saying that the two variables are NSStrings but you aren't assigning NSStrings to them. You need to assign NSString objects to NSString variables - they aren't created for you automatically.
So the answers which are telling you to use formatted strings are correct. You really should be doing it like this:
partenaire_lat = [[NSString stringWithFormat:#"%f", 48.8160525] retain];
partenaire_lng = [[NSString stringWithFormat:#"%f", 2.3257800] retain];
what are lat and lng? i'm assuming float or double..so you should use [NSString stringWithFormat:#"%f,%f", lat, lng]; (or however you want the floats to be formatted)
You code has several potential problems:
%# format specifier expects object parameter, while it looks like you pass plain float (I may be wrong here as there's not enough context to be sure). Change format to %f to fix your problem if that's really the case:
NSString *endPoint1 =[NSString stringWithFormat:#"%f,%f", partenaire_lat, partenaire_lng];
Your endPoint1 string is autoreleased and may become invalid outside of current scope if you don't retain it. So if you try to use your variable in another method you probably should retain it.
All you need to do
NSString *latStr=[[NSNumber numberWithFloat:partenaire_lat] stringValue];
NSString *lngStr=[[NSNumber numberWithFloat:partenaire_lng] stringValue];
and do whatever you want to do with these two string :)

How to selectively trim an NSMutableString?

I would like to know how to selectively trim an NSMutableString. For example, if my string is "MobileSafari_2011-09-10-155814_Jareds-iPhone.plist", how would I programatically trim off everything except the word "MobileSafari"?
Note : Given the term programatically above, I expect the solution to work even if the word "MobileSafari" is changed to "Youtube" for example, or the word "Jared's-iPhone" is changed to "Angela's-iPhone".
Any help is very much appreciated!
Given that you always need to extract the character upto the first underscore; use the following method;
NSArray *stringParts = [yourString componentsSeparatedByString:#"_"];
The first object in the array would be the extracted part you need I would think.
TESTED CODE: 100% WORKS
NSString *inputString=#"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";
NSArray *array= [inputString componentsSeparatedByString:#"_"];
if ([array count]>0) {
NSString *resultedString=[array objectAtIndex:0];
NSLog(#" resultedString IS - %#",resultedString);
}
OUTPUT:
resultedString IS - MobileSafari
If you know the format of the string is always like that, it can be easy.
Just use NSString's componentsSeparatedByString: documented here.
In your case you could do this:
NSString *source = #"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";
NSArray *seperatedSubStrings = [source componentsSeparatedByString:#"_"];
NSString *result = [seperatedSubStrings objectAtIndex:0];
#"MobileSafari" would be at index 0, #"2011-09-10-155814" at index 1, and #"Jareds-iPhone.plist" and at index 2.
Try this :
NSString *strComplete = #"MobileSafari_2011-09-10-155814_Jareds-iPhone.plist";
NSArray *arr = [strComplete componentsSeparatedByString:#"_"];
NSString *str1 = [arr objectAtIndex:0];
NSString *str2 = [arr objectAtIndex:1];
NSString *str3 = [arr objectAtIndex:2];
str1 is the required string.
Even if you change MobileSafari to youtube it will work.
So you'll need an NSString variable that'll hold the beginning of the string you want to truncate. After that one way could be to change the string and the variable string values at the simultanously. Say, teh Variable string was "Youtube" not it is changed to "MobileSafari" then the mutable string string should change from "MobileSafari_....." to "YouTube_......". And then you can get the variable strings length and used the following code to truncate the the mutable string.
NSString *beginningOfTheStr;
.....
theMutableStr=[theMutableStr substringToIndex:[beginningOfTheStrlength-1]];
See if tis works for you.

NSString - how to go from "ÁlgeBra" to "Algebra"

Does anyone knows hoe to get a NSString like "ÁlgeBra" to "Algebra", without the accent, and capitalize only the first letter?
Thanks,
RL
dreamlax has already mentioned the capitalizedString method. Instead of doing a lossy conversion to and from NSData to remove the accented characters, however, I think it is more elegant to use the stringByFoldingWithOptions:locale: method.
NSString *accentedString = #"ÁlgeBra";
NSString *unaccentedString = [accentedString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
NSString *capitalizedString = [unaccentedString capitalizedString];
Depending on the nature of the strings you want to convert, you might want to set a fixed locale (e.g. English) instead of using the user's current locale. That way, you can be sure to get the same results on every machine.
NSString has a method called capitalizedString:
Return Value
A string with the first character from each word in the receiver changed to its corresponding uppercase value, and all remaining characters set to their corresponding lowercase values.
NSString *str = #"AlgeBra";
NSString *other = [str capitalizedString];
NSLog (#"Old: %#, New: %#", str, other);
Edit:
Just saw that you would like to remove accents as well. You can go through a series of steps:
// original string
NSString *str = #"ÁlgeBra";
// convert to a data object, using a lossy conversion to ASCII
NSData *asciiEncoded = [str dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
// take the data object and recreate a string using the lossy conversion
NSString *other = [[NSString alloc] initWithData:asciiEncoded
encoding:NSASCIIStringEncoding];
// relinquish ownership
[other autorelease];
// create final capitalized string
NSString *final = [other capitalizedString];
The documentation for dataUsingEncoding:allowLossyConversion: explicitly says that the letter ‘Á’ will convert to ‘A’ when converting to ASCII.
Here's a step by step example of how to do it. There's room for improvement, but you get the basic idea......
NSString *input = #"ÁlgeBra";
NSString *correctCase = [NSString stringWithFormat:#"%#%#",
[[input substringToIndex:1] uppercaseString],
[[input substringFromIndex:1] lowercaseString]];
NSString *result = [[[NSString alloc] initWithData:[correctCase dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES] encoding:NSASCIIStringEncoding] autorelease];
NSLog( #"%#", result );

How do i put an XML source into an NSString?

I've done this with HTML, like so:
NSString *theweatherhtml = [[NSString alloc] initWithContentsOfURL:[NSURL URLWithString:#"*URL STRING WOULD GO HERE*"]];
But when i do this with an XML file, it returns a 'Program received signal: “EXC_BAD_ACCESS”.'
Is there a different way to do this for XML files?
That method is deprecated. You need to use:
initWithContentsOfURL:encoding:error:
http://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSString_Class/Reference/NSString.html
You can use NSMutableDictionary using objectforkey Or NSArray and then convert each value into related varibles. Like -
int value;
value = [[savedStock objectForKey:#"userID"] intValue];

Using NSString stringWithFormat: with a string from NSDictionary

I'm getting an NSString from a dictionary that needs to have a variable integer, something like:
"You have %i objects."
How do I put the calculated integer value into the string? I would like to do something like
NSString *string = [NSString stringWithFormat:[dictionary objectForKey:#"string"]
But I don't know how to pass in an argument to stringWithFormat when the %i is tucked away in the dictionary.
Note: I can work around this by using stringByReplaceOccurenceOfString, but I'd like to know if it's possible to do it in the above way.
You can pass a comma separated list of arguments to methods like stringWithFormat:
NSString *string = [NSString stringWithFormat:
[dictionary objectForKey:#"string"] , 7 , 8 , 9];
Only the 7 would be used in your sample format string.
You can access it using this
NSString *string = [NSString stringWithFormat:[dictionary objectForKey:#"string"],YOURINTNUMBER];
It will work