I have google some related questions, but unfortunately didn't find answer.
I have string like 2016-07-22, i need to get an integer 07, evaluate it to 7 and save.
Of course, this is date, therefore it will change every time, so i cant suppose that year always will be 2016. I need to get string after 5th symbol up to 8th.
Is there any easy way to achieve that? Thanks.
If it's always that section of the string, you can use NSMakeRange(5, 2) and substringWithRange to pick out the month.
After you have "07", conversion is just a case of asking the string for its integerValue.
The "save" part depends entirely on where you want to save it.
Here is just one way (I can think of at least 3 other ways):
NSString *str = #"2016-07-22";
NSArray *elements = [str componentsSeparatedByString:#"-"];
NSAssert([elements count] == 3, #"Ahhh!");
NSInteger month = [elements[1] integerValue];
Related
I have a sqlite database as columns groupno, sunday, monday, tuesday, wednesday, thursday, friday and saturday.
I have retrieved the data using the following code. But this code gives all data in a single column (for e.g. code skips all previous values in a column and displays final item at the end of the column). However what I need is data from a specific row.
I do not know how to access a single item from NSArray. I would be really grateful for any help.
Thanks.
NSArray *info = [dba getinfo];
for(Getinfo *p in info)
getinfo.text = [p sunday ];
Thanks everyone,
I was able to solve the problem by following code to select the specific item,
id schedule = [info objectAtIndex:0];
_displayinfo.text = [schedule sunday];
Thanks again
I have a screen where the user can enter some information. It shows a decimalPad. According to the user locale, I know the decimalPadwill show commaor period, or even something else.
Therefore, my database will contain NSStrings stored from the users, although, some of them will have comma, like 5,4, or period, as in 5.4. The problem is that I need to do some math with these values, and I can't find a pattern to convert them.
Lets say the NSStringhas a comma, as in 5,4. I can easily convert that to a floatusing:
NSNumberFormatter *commas = [NSNumberFormatter new];
commas.numberStyle = NSNumberFormatterDecimalStyle;
NSLocale *commaLocale = [[NSLocale alloc] initWithLocaleIdentifier: #"br_PT"];
commas.locale = commaLocale;
NSString *string = #"5,4";
float testFloat = [[commas numberFromString:string] floatValue];
I correctly get the result 5.4, and can go on with my math calculations. But for instance, if the number was saved by an US user, the NSString would be 5.4, and I wouldn't get the right result using this code. Even if I get user current locale, it doesn't help me, since the user will get values stored all around.
Anyway, I'm a little confused with this. How could I simply create a pattern to deal with this?
I don't want to work with the string, as in stringByReplacingOccurences..., anyway, at least if I can avoid that.
If you want to use the value as a number, you should store the value as a number in your database. NSNumber is a perfect object for that and with your method you can get correct value. When you show the value again to the user you should convert the NSNumber again with NSNumberFormatter. Everything else will be a workaround and have negative impact. Even with stringByReplacingOccurences... you have the risk that somebody uses thousand-dots or commas...
I've got an array of NSDates and I'd like to grab the largest NSDate from the array. While i could always sort them and grab the first/last, is there a way to do this with KeyValueCoding or some other quick one liner kind of way? I know that I could use something like valueForKeyPath#"#max.date" if the objects had a date property, but what if the objects are dates themselves??
thanks
You can use,
NSDate *maxDate = [dateArray valueForKeyPath:#"#max.self"];
This will give you the largest date from array. You dont have to sort the array before doing this.
From the documentation,
The #max operator compares the values of the property specified by the
key path to the right of the operator and returns the maximum value
found. The maximum value is determined using the compare: method of
the objects at the specified key path. The compared property objects
must support comparison with each other. If the value of the right
side of the key path is nil, it is ignored.
Note that #max will do compare: and then will find out the max value.
Agree with #SeanDanzeiser. To be more specific, here's a ~70 byte one-liner:
// if dateArray is the array of dates ...
NSDate *maxDate = [[dateArray sortedArrayUsingSelector:#selector(compare:)] lastObject];
i want know if is possible, to get a specific element value of a xml file without use all delegate NSXMLParse method, i know that in OS X is possible with two simple line:
NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithData:connection.data options:0 error:nil];
NSString *time = [[xmlDoc rootElement] stringValueForXPath:#"./Time"];
is possible do the same thing in iOS? with few line?
thanks
XPath exists for iOS, as well. Here is a really great tutorial on using libxml2, XPath, and XML for parsing data.
Coca with Love
With XPath, you're expected to pass in a clip query. For the following, XML, your clip query would be "//day":
<day>
Monday
</day>
<day>
Tuesday
</day>
...
<day>
Friday
</day>
This will give you an array of 5 dictionaries with node_name = day and node_content = the day of the week.
I have a label which will show the text which I have added using the below code
m_label = [[[NSString stringWithFormat:#"%d",retValue]stringByAppendingString:#" "]stringByAppendingString:NSLocalizedString(#"DAYS",nil)];
So here the text will be displayed in this format: 10(space)days.
I have localised the "days" string,I want to know whether we should localise a space or blank string,
Regards
Ranjit
I think this depends on the languages you are targeting, in the end.
I don't see any particular risks with the expression "10 days", in almost any language I know the number of days would be separated from the word "days" by a space. Of course, I don't know all the languages in the world.
Just an opinion.
Why not simply:
m_label = [NSString stringWithFormat:#"%d %#",
retValue, NSLocalizedString(#"DAYS",nil)];
If the need arises, you can localize the format string. That way, you will not only get flexibility on whitespace, but also e.g., on ordering: putting "DAYS" before their count.