Converting a user's phone number into english numbers - ios

I have an application that user must insert his number and send it to the server.
The problem that i am encountering is that some users insert their numbers in their native language(For example Urdu, arabic, Indian or others)
What I want is to convert all numeric numbers from different languages to English numbers(1,2,3...) and then send it to server.
Is there a possible way to achieve that?
Thank you in advance.

I'd be surprised if you can't just do...
NSInteger blah = [enteredString intValue];
// you will have to know if it's an int, float, double, etc...
// the entered number is still a number just using a different font (I guess).
// the shape of the number 2 comes entirely from the font so I don't see why this wouldn't work
But if that doesn't work take a look at the NSNumberFormatter class. You should be able to do something like...
NSNumberFormatter *nf = [NSNumberFormatter new];
NSNumber *number = [nf numberFromString:enteredString];
Either way should work. Try the first one first. If that doesn't work then give the number formatter a go. You may have to set the locale of the number formatter.
Tested with a working project
// This is the only code.
#import "ViewController.h"
#interface ViewController () <UITextFieldDelegate>
#property (weak, nonatomic) IBOutlet UITextField *textField;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self getNumber];
return YES;
}
- (void)getNumber
{
NSInteger number = [self.textField.text intValue];
NSLog(#"%ld", (long)number);
}
#end
I changed the simulator language to Arabic and it worked perfectly.
Screenshot...
Code...
Literal string vs. entered string
I'm guessing this is because your development language is English.
Anyway, when you enter the literal string ١‎٢‎٣ into Xcode and store it in a string it is different from taking the string ١‎٢‎٣ from an Arabic textfield...

Related

watch kit extension connect using SQLite

I need help how to connect my SQLite database to my watchkit app extension. Im not much familiar in using sqlite with cell rows. Any easy sample codes will be a great help thanks. Below are sample arrays i used for example.
#import "ICBQuoteSource.h"
#implementation ICBQuoteSource
+(NSArray *)quoteDictionary {
NSMutableArray *quotes = [NSMutableArray new];
[quotes addObject:#{#"characterImage": #"moss", #"characterName": #"Moss", #"quote": #"I came here to drink milk and kick ass... and I've just finished my milk."}];
[quotes addObject:#{#"characterImage": #"roy", #"characterName": #"Roy", #"quote": #"Hello, IT, have you tried turning it off and on again?"}];
[quotes addObject:#{#"characterImage": #"moss", #"characterName": #"Moss", #"quote": #"Did you see that ludicrous display last night?"}];
[quotes addObject:#{#"characterImage": #"denholm", #"characterName": #"Denholm", #"quote": #"That's the sort of place this is, Jen. A lot of sexy people not doing much work and having affairs."}];
[quotes addObject:#{#"characterImage": #"moss", #"characterName": #"Moss", #"quote": #"This Jen is the Internet"}];
return [NSArray arrayWithArray:quotes];
}
#end
#import "InterfaceController.h"
#import "ICBQuoteSource.h"
#import "rowController.h"
#interface InterfaceController()
#property (nonatomic, strong) NSArray *quotes;
#end
#implementation InterfaceController
- (void)awakeWithContext:(id)context
{
[super awakeWithContext:context];
// Get quotes
self.quotes = [ICBQuoteSource quoteDictionary];
// Set number of table Row
[self.table setNumberOfRows:self.quotes.count withRowType:#"Row Controller"];
// Set row properties
for (NSDictionary *quote in self.quotes) {
rowController *quoteRow = [self.table rowControllerAtIndex:[self.quotes indexOfObject:quote]];
[quoteRow.englishTxtLabel setText:quote[#"characterName"]];
[quoteRow.translationTxtLabel setText:quote[#"quote"]];
}
}
- (void)willActivate {
// This method is called when watch view controller is about to be visible to user
[super willActivate];
}
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}
#end
You haven't actually stated what the problem is...so let me attempt to guess. I'm assuming that the rows are not showing up properly in your table. What I would suggest trying is moving your row set up logic into willActivate. This is something I found when working with tables in Xcode 6.2b5. You would not get the appropriate behavior in awakeWithContext(:).
Hopefully that helps lead you in the right direction. If this isn't the actual problem you are having, then please edit your question and I'll edit my answer accordingly.

I built a simple iOS calculator but I can not figure out how to add a decimal point

I built a simple calculator app and am struggling to add the decimal point button,
when I push the decimal button it will add the decimal but after I enter another number the
decimal point disappears so something is for sure wrong.
If someone can specifically walk me trough how to fix the issue since I am a beginner and get the decimal to work I would greatly appreciate it.
Here is my .h file
#import <UIKit/UIKit.h>
int Method;
int SelectNumber;
float RunningTotal;
int currentNumber;
#interface ViewController : UIViewController
{
IBOutlet UILabel * Screen;
}
-(IBAction)Number1:(id)sender;
-(IBAction)Number2:(id)sender;
-(IBAction)Number3:(id)sender;
-(IBAction)Number4:(id)sender;
-(IBAction)Number5:(id)sender;
-(IBAction)Number6:(id)sender;
-(IBAction)Number7:(id)sender;
-(IBAction)Number8:(id)sender;
-(IBAction)Number9:(id)sender;
-(IBAction)Number0:(id)sender;
-(IBAction)Times:(id)sender;
-(IBAction)Devide:(id)sender;
-(IBAction)Subtract:(id)sender;
-(IBAction)PLus:(id)sender;
-(IBAction)Equalls:(id)sender;
-(IBAction)AllClear:(id)sender;
-(IBAction)Decimal:(id)sender;
#end
here is the code for the decimal in the .m file
- (IBAction)Decimal:(UIButton *)sender
{
NSRange range = [self->Screen.text rangeOfString:#"."];
if (range.location ==NSNotFound){
self->Screen.text = [ self->Screen.text stringByAppendingString:#"."];
}
self.userIsInTheMiddleOfEnteringANumber = YES;
}
Your are printing your result as integer, change it to float %f, and u can even choose how many of decimals you need: %.02f = 25.00

I get this "Thread 1: signal SIGABRT"

I can't figure out why I keep getting this error. Whenever I press the button in my app the whole thing crashes. Here is the code:
#import "additionViewController.h"
#interface additionViewController (){
}
#end
#implementation additionViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getValue: (id)sender {
self.numberOne = (NSNumber *)self.firstNum.text;
self.numberTwo = (NSNumber *)self.secondNum.text;
self.fnumberOne = [self.numberOne floatValue];
self.fnumberTwo = [self.numberTwo floatValue];
self.finalValue = [NSNumber numberWithFloat:(self.fnumberOne + self.fnumberTwo)];
self.sum.text = (NSString *)self.finalValue;
}
#end
This is the first iOS app I'm trying to make by myself. It's pretty simple. Not going on the App Store or anything.
Assuming firstNum and secondNum are UITextFields - the text properties of those objects will be NSStrings. You can't convert them to numbers by doing a cast to NSNumber. This will just change the type of the pointer but do nothing to actually convert the representation of the data from text to a number.
The simplest solution is:
self.fnumberOne = self.firstNum.floatValue;
self.fnumberTwo = self.secondNum.floatValue;
The floatValue properties will actually convert the text to a float. Then perhaps you don't even need the self.numberOne and self.numberTwo properties? If you still do, you can do this (and similarly with self.finalNumber if you need that too):
self.fnumberOne = self.firstNum.floatValue;
self.fnumberTwo = self.secondNum.floatValue;
self.numberOne = #(self.fnumberOne);
self.numberTwo = #(self.fnumberTwo);
The #() syntax is a convenient way to wrap a primitive in its associated object type, in this case a NSNumber.
Also, you'll have a simlar problem with self.sum.text = (NSString *)self.finalValue - you can't use a C casting operator, you need to actually convert the value back to a string. Simple solution is:
self.sum.text = [NSString stringWithFormat:#"%f", self.fnumberOne + self.fnumberTwo];
There are more advanced ways to convert to/from numeric values using NSNumberFormatter, etc., if you need more robust support. There's plenty of documentation on that.
Go into the project navigator (left window) and go into the breakpoints tab (looks like a carrot) then add an exception breakpoint. This breakpoint should show you where the crash occured. I suspect the problem is trying to cast an NSString to an NSNumber. Use intValue, floatValue, or doubleValue instead.
you need to deallocate the memory you used for these variables
release variableName;
for example, Im not sure what type numberOne, and numberTwo are

Is there anyway I can compare a String (which is a word) and a letter which is input by the user and receive an output as a BOOL

I'm new to IOS dev and am making simple programs this one is a hangman game.
I wanted to pick a random string from a plist file (completed).
I now want to compare the user input text (from a text field) and compare it to the string we have randomly picked from our plist.
Here is my code for MainViewController.m as it is a utility. Only the MainView is being used currently.
#import "MainViewController.h"
#import "WordListLoad.h"
#interface MainViewController ()
#end
#implementation MainViewController
#synthesize textField=_textField;
#synthesize button=_button;
#synthesize correct=_correct;
#synthesize UsedLetters=_UsedLetters;
#synthesize newgame=_newgame;
- (IBAction)newg:(id)sender
{
[self start];
}
- (void)start
{
NSMutableArray *swords = [[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"swords" ofType:#"plist"]];
NSLog(#"%#", swords);
NSInteger randomIndex = arc4random() % [swords count];
NSString *randomString = [swords objectAtIndex:randomIndex];
NSLog(#"%#", randomString);
}
This is where i would like to implement the checking
I have tried characterAtIndex and I can't seem to get it to work for hard coded placed in the string let along using a for statement to systematic check the string.
- (void)check: (NSString *) randomString;
{
//NSLogs to check if the values are being sent
NSLog(#"2 %#", self.textField.text);
}
- (IBAction)go:(id)sender
{
[self.textField resignFirstResponder];
NSLog(#"1 %#", self.textField.text);
[self check:(NSString *) self.textField];
_textField.text = nil;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self start];
}
To compare 2 strings: [string1 equalsToString:string2]. This will return true if string1 is equal to string2. To get the string contained in a UITextfield: textfield.text.
Given that it's a hangman game, I assume you are trying to see if a single letter is contained by a given string - so equalsToString: wouldn't be what you want.
Instead, probably better to use rangeOfString:options:
if ([randomString rangeOfString:self.textfield.text options:NSCaseInsensitiveSearch].location != NSNotFound){
// Do stuff for when the letter was found
}
else {
// Do stuff for when the letter wasn't found
}
Also, as was pointed out by Patrick Goley, you need to make sure you're using the textfield.text value to get the string from it. Same with storing the initial word you'll be using as the hidden word.
There are also a couple of other minor code issues (semicolon in the function header, for example) that you'll need to clean up to have a functioning app.
Edit: Made the range of string call actually use the textfield's text, and do so case-insensitive (to prevent false returns when a user puts a capital letter when the word is lower case, or vice-versa). Also included link to documentation of NSString's rangeOfString:options:
For your check method you are sending the UITextfield itself, instead of its text string. Instead try:
[self check: self.textfield.text];
You'll also need to create an NSString property to save your random string from the plist, so you can later access to compare to the textfield string like so:
declare in the interface of the class:
#property (nonatomic,strong) NSString* randomString;
in the start method:
self.randomString = [swords objectAtIndex:randomIndex];
in the check method:
return [self.randomString isEqualToString:randomString];

Overriding "text" in UILabel does not work in iOS 6

My class "TypographicNumberLabel" is a subclass of UILabel. This class overrides the "text" setters and getters of UILabel with the purpose to produce nicely rendered numbers in a table. For instance, it can add some extra white space for right alignment, unary plus signs, append units, etc.
My problem is that this class has worked perfectly fine up to iOS 5.1, but in iOS 6, it has stopped working: It is now rendering exactly as the standard UILabel (but when its properties are accessed from code, they are still giving correct results).
Since this class is used in a huge mass of legacy code, I would really like to repair my original code instead of rewriting it using completely new methods. So, please focus your answers on explaining how to override "-text" and "-setText:" for UILabel in iOS 6.
This is (a simplified version of) my code:
#interface TypographicNumberLabel : UILabel {
NSString *numberText;
}
// PROPERTIES
// "text" will be used to set and retrieve the number string in its original version.
// integerValue, doubleValue, etc. will work as expected on the string.
// The property "text" is declared in UILabel, but overridden here!
// "typographicText" will be used to retrieve the string exactly as it is rendered in the view.
// integerValue, doubleValue, etc. WILL NOT WORK on this string.
#property (nonatomic, readonly) NSString* typographicText;
#end
#implementation TypographicNumberLabel
- (void) renderTypographicText
{
NSString *renderedString = nil;
if (numberText)
{
// Simplified example!
// (Actual code is much longer.)
NSString *fillCharacter = #"\u2007"; // = "Figure space" character
renderedString = [fillCharacter stringByAppendingString: numberText];
}
// Save the typographic version of the string in the "text" property of the superclass (UILabel)
// (Can be retreived by the user through the "typographicText" property.)
super.text = renderedString;
}
#pragma mark - Overridden UILabel accessor methods
- (NSString *) text
{
return numberText;
}
- (void) setText:(NSString *) newText
{
if (numberText != newText)
{
NSString *oldText = numberText;
numberText = [newText copy];
[oldText release];
}
[self renderTypographicText];
}
#pragma mark - TypographicNumberLabel accessor methods
- (NSString *) typographicText
{
return super.text;
}
#end
Example of use (aLabel is loaded from .xib file):
#property(nonatomic, retain) IBOutlet TypographicNumberLabel *aLabel;
self.aLabel.text = #"12";
int interpretedNumber = [self.aLabel.text intValue];
This type of code works perfectly fine in both iOS 5.1 and in iOS 6, but the rendering on screen is wrong in iOS 6! There, TypographicNumberLabel works just like a UILabel. The "figure space" character will not be added.
The issue is at
- (NSString *) text
{
return numberText;
}
You can see the method ([self text]) is called internally, so it's better to return the text you want to be shown, otherwise you can easily ruin internal control logic:
- (NSString *) text
{
return [super text];
}
After having submitted my question, I found a solution myself. Perhaps not the definite solution, but at least a useful workaround. Apparently, the rendering logic of UILabel has changed when attributedText was introduced in iOS 6. I found that setting the attributedText property instead of super.text will work.
To be more specific:
The following line in renderTypographicText
super.text = renderedString;
should be replaced with
if (renderedString && [UILabel instancesRespondToSelector: #selector(setAttributedText:)])
super.attributedText = [[[NSAttributedString alloc] initWithString: renderedString] autorelease];
else
super.text = renderedString;
then the rendering works fine again!
This is a bit "hackish", I admit, but it saved me from rewriting a huge amount of legacy code.

Resources