I have this in my code:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *currency = [defaults objectForKey:#"currencySymbol"];
NSString *text = [textField.text stringByReplacingCharactersInRange:range withString:string];
text = [text stringByReplacingOccurrencesOfString:#"." withString:#""];
text = [text stringByReplacingOccurrencesOfString:currencySymbol withString:#""];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
[numberFormatter setCurrencySymbol:currency];
NSNumber *number = [NSNumber numberWithDouble:[text intValue]*0.01];
text = [numberFormatter stringFromNumber:number];
textField.text = text;
and it works great until you reach a certain amount, then it can't go higher, for example:
2,500.00 (is possible)
25,000.00 (it´s not possible)
Maybe it´s something stupid, could anyone alert me what is that i am not seeing?
Thanks.
For those who are having the same problem, we have to add this line of code:
text = [text stringByReplacingOccurrencesOfString:#"," withString:#""];
Because (logically) the , is used for thousands...
Related
I am trying to show the User different Phone Numbers on Apple Watch and he clicks on one than phone call alert should appear. I'll do it like this but the Alert is just dismissed without call action:
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithCapacity:0];
WKExtension *myExt = [WKExtension sharedExtension];
for (NSString *phone in arr) {
NSString *tel = [NSString stringWithFormat:#"tel:%#",phone];
WKAlertAction *act = [WKAlertAction actionWithTitle:tel style:WKAlertActionStyleDefault handler:^(void){
[myExt openSystemURL:[NSURL URLWithString:phone1]];
}];
[tempArray addObject:act];
}
NSString *titleMessage = #"Call";
NSString *textMessage = #"Please select the number you want to call.";
NSString *cancel = #"Cancel";
WKAlertAction *act = [WKAlertAction actionWithTitle:cancel style:WKAlertActionStyleDestructive handler:^(void){
}];
[tempArray addObject:act];
[self presentAlertControllerWithTitle:titleMessage message:textMessage preferredStyle:WKAlertControllerStyleAlert actions:tempArray];
Buttons are shown as expected and the Handler is also called with the correct Phone Number. But it does not openSystemURL. Does somebody know why and how to fix? Thanks!
I think you forgot to add "tel" scheme ,Use below code :
[WKAlertAction actionWithTitle:#"tel" style:WKAlertActionStyleDefault handler:^(void){
[[WKExtension sharedExtension]openSystemURL:[NSURL URLWithString:[NSString stringWithFormat:#"tel:%#",#"YOUR NUMBER"]]];
}];
About Apple URL Schemes
I need to convert a NSString object of format "Tuesday 14th October" to NSDate, of format "Tuesday, 14 Oct". This is what I tried,
NSString *day = #"Tuesday 14th October";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:#"EEEE, dd MMM"];
NSArray *dateComponets = [day componentsSeparatedByString:#" "];
NSString *dayOfTheWeek = dateComponets[0];
NSString *date = [dateComponets[1] substringToIndex:2];
NSString *month = [dateComponets[2] substringToIndex:3];
NSString *newDay = [NSString stringWithFormat:#"%#, %# %#", dayOfTheWeek,date, month];
NSDate *newDate = [dateFormatter dateFromString:newDay];
NSLog(#"date : %#", [dateFormatter stringFromDate:newDate]);
It seems to work but I felt its a bit clunky..do you guys know more elegant/better/standard way to do this in iOS?
Store an NSDate in Core Data - So:
NSString *day = #"Tuesday 14th October";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE dd MMM"];
myCoreDataObject.savedDate = [dateformatter dateFromString: day];
Then do the reverse when you want to display it:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"EEEE, dd MMM"];
myLabel.text = [dateformatter stringFromDate: myCoreDataObject.savedDate];
Don't actually allocate a new data formatter each time, its really slow.
The locale will default to the system one, so you don't need to explicitly set it necessarily
There is no such thing as an "NSDate of firmat X"; you've fundamentally failed to understand NSDate. It's a timestamp. It identifies an objective instant in time. It has no format.
Just use NSDateFormatter for whatever it is you actually want to do. It can parse a string into a date and vice versa. Don't write your own hacky text parsing. Don't touch NSDateComponents at all.
This is a weird one. I've got a label on my storyboard in a custom prototype cell whose tag is 102. As the subject title suggests, I can set the text of the label in various ways and it appears on the device as expected. Here are the methods I've tried that work:
nameLabel.text = #"My buddy's name";
nameLabel.text = [[self.matchArray objectAtIndex:indexPath.row] valueForKey:#"player2Username"];
[nameLabel setText:[[self.matchArray objectAtIndex:indexPath.row] valueForKey:#"player2Username"]];
These, however, don't work...
NSString *nameString = [NSString stringWithFormat:#"game with %#", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:#"player2Username"]];
[nameLabel setText:nameString];
also...
[nameLabel setText:[NSString stringWithFormat:#"game with %#", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:#"player2Username"]]];
and...
nameLabel.text = [NSString stringWithFormat:#"game with %#", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:#"player2Username"]];
I need for the cells to read: "game with so-and-so" where "so-and-so" is the opponent's name, obviously. Any ideas as to what would cause this?
EDIT: So here's some more code, demonstrating how I'm attacking this thing:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MatchCell"];
UILabel *nameLabel = (UILabel *)[cell viewWithTag:102];
...
}
if (indexPath.section == 0) {
nameLabel.text = [[self.matchArray objectAtIndex:indexPath.row] valueForKey:#"player2Username"];
[nameLabel setText:[[self.matchArray objectAtIndex:indexPath.row] valueForKey:#"player2Username"]];
NSString *nameString = [NSString stringWithFormat:#"game with %#", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:#"player2Username"]];
[nameLabel setText:nameString];
[nameLabel setText:[NSString stringWithFormat:#"game with %#", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:#"player2Username"]]];
nameLabel.text = [NSString stringWithFormat:#"game with %#", [[self.matchArray objectAtIndex:indexPath.row] valueForKey:#"player2Username"]];
}
return cell;
}
I'm using Parse as my BaaS. There really doesn't seem to be a problem there, as I can retrieve, log, and display on my iPhone (in the cell) the player's username and any other data I want, as long as I don't incorporate stringWithFormat:.
I tried cleaning the project and the build folder, uninstalling from the device, etc. Re-ran it. Still no luck.
Newb mistake. I didn't size the UILabel wide enough to accommodate the extra text. That is to say I could explicitly set a string (e.g. nameLabel.text = #"player name";) and there was enough room to fit the player's name, but whenever I tried to prepend "game with " to any user's name, it had the effect of making the entire string too long for the small width of the UILabel to accommodate. So, I guess it was reverting to the text of the label (which I had set in Interface Builder/Storyboard), "game with...".
In a nutshell, be sure you make your UILabels wide enough to accommodate the text they're going to be handling, kids! If you don't, someone's gonna wind up with a disease or pregnant or worse. I've seen it a thousand times.
I have an application where i am storing japanese text in a coredata sqlite database. Populating the data seems to work ok (as far as I can tell), but when I search on any string field it returns no records. If I search on one of my integer fields it does.
The string data is unicode (ie, Japanese characters), so my question is will core data not work with Unicode? Seems unlikely. Does something special have to be done so a search (fetch) will work?
Code to populate the data looks like this:
WordProgress *newWordProgress = [NSEntityDescription insertNewObjectForEntityForName:#"WordProgress" inManagedObjectContext:progressContext];
newWordProgress.kanji = kanji;
newWordProgress.kanji = kana;
newWordProgress.senseIndex = [NSNumber numberWithInt:senseIndex];
newWordProgress.skillType = [NSNumber numberWithInt:skillType];
newWordProgress.leitnerDeck = leitnerDeck;
newWordProgress.dateLastShown = lastShownDate;
Code to test my fetch, looks like this:
NSPredicate *pred = [NSPredicate predicateWithFormat:#"kanji = %#", #"彼"];
NSFetchRequest *testRequest = [[NSFetchRequest alloc] init];
[testRequest setPredicate:pred];
[testRequest setEntity:[NSEntityDescription entityForName:#"WordProgress" inManagedObjectContext:progressContext]];
NSArray *results = [progressContext executeFetchRequest:testRequest error:&error];
But returns no records, when I know that records were populated.
Just tried this too, and even this seems to fail. I'm sure I must be doing something fundamentally wrong:
WordProgress *newWordProgress = [NSEntityDescription insertNewObjectForEntityForName:#"WordProgress" inManagedObjectContext:progressContext];
newWordProgress.kanji = #"彼";
newWordProgress.kanji = kana;
newWordProgress.senseIndex = [NSNumber numberWithInt:senseIndex];
newWordProgress.skillType = [NSNumber numberWithInt:skillType];
newWordProgress.leitnerDeck = leitnerDeck;
newWordProgress.dateLastShown = lastShownDate;
NSPredicate *pred = [NSPredicate predicateWithFormat:#"kanji = %#", #"彼"];
NSFetchRequest *testRequest = [[NSFetchRequest alloc] init];
[testRequest setPredicate:pred];
[testRequest setEntity:[NSEntityDescription entityForName:#"WordProgress" inManagedObjectContext:progressContext]];
NSArray *results = [progressContext executeFetchRequest:testRequest error:&error];
It's not a Unicode problem, it's an NSPredicate error that would affect any string. You have:
NSPredicate *pred = [NSPredicate predicateWithFormat:#"kanji = '%#'", #"彼"];
The single quotes in there are the problem. You're creating a predicate that looks for a literal %# in the data. The single quotes prevent substituting the #"彼" for the %#. Take the single quotes out and it should work as expected:
NSPredicate *pred = [NSPredicate predicateWithFormat:#"kanji = %#", #"彼"];
My god, I'm completely retarded, sorry. Doing some more playing I saw the line...
newWordProgress.kanji = kanji;
newWordProgress.kanji = kana;
Doh! It was completely overwriting my key field. It should have read...
newWordProgress.kanji = kanji;
newWordProgress.kana = kana;
Thanks for the replies, guys. Sorry!
I got a question for anyone with a spare moment and an answer...
I need to change an NSString value to input into the AVAudioPlayer start offset
eg. player.currentTime = 0; I want to change the '0' to a readable value or an NSString to a numeral value.....
NSString has a integerValue function. You can also create an NSString using it's format class function and pass it a format string like this: "%d" and then your player.currentTime.
int timeInput = [[textField text] integerValue];
label.text = [NSString stringWithFormat:#"%d", timeInput];