Can't save or properly dial phonenumber, too big ios [duplicate] - ios

This question already has answers here:
NSString intValue not working for retrieving phone number
(2 answers)
Closed 8 years ago.
Hi when I try to save a phonenumber with this line to a coredata element
[newLead setValue:[NSNumber numberWithInteger:[self.cellTextField.text doubleValue] ] forKey:#"cell"];
Then I load it with this
[self.cellTextField setText:[[self.lead valueForKey:#"cell"] stringValue]];
It works but if the number is like 905666777 , that number is too big and i get 2147483647
How can i make it use a 905 number?
also when i dial the number using
NSURL *telUrl = [NSURL URLWithString:[#"tel://" stringByAppendingString:#"cell"]];
[[UIApplication sharedApplication] openURL:telUrl];
It only dials the first 3 digits 214, how can i make it dial all the digits
Thank you

The problem is that you're saving a phone number as a numeric value, so you're running into limits on numeric types. There's no reason to do this this-- you should be using a string. You'll never do math on the phone number, for example. Any fetches you might do (for example finding phone numbers with a specific area code) will be more difficult with a numeric type than with a string.

Related

Call Directory Extension CallKit does't recognize numbers with more than 9 digits

I'm working with CallKit and developing an app with a Call Directory Extension. I've followed this tutorial and I'm currently test the capability of identify numbers that the user does't have in his contacts and show an ID from my app, but although is working perfectly with numbers of 1 to 9 digits, for example 123456, when I set numbers with 10 or more digits, iOs doesn't recognize the number. After a day and a half of google it, I've have found no information about that. If anyone can help me I'll appreciate it. Thanks in advance.
The method for set the phone numbers for recognize:
private func addAllIdentificationPhoneNumbers(to context: CXCallDirectoryExtensionContext) {
// Retrieve phone numbers to identify and their identification labels from data store. For optimal performance and memory usage when there are many phone numbers,
// consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded.
//
// Numbers must be provided in numerically ascending order.
let allPhoneNumbers: [CXCallDirectoryPhoneNumber] = [ 123456789, 1_888_555_5555 ]
let labels = [ "ID test", "Local business" ]
for (phoneNumber, label) in zip(allPhoneNumbers, labels) {
context.addIdentificationEntry(withNextSequentialPhoneNumber: phoneNumber, label: label)
}
}
With this code, when I simulate a call with the number 123456789, iOS shows the tag "ID test" and that's correct, but if I add any digit, for example 0 at the end: 1234567890, iOS does't show anything when I simulate a call. I don't know if I'm missing something.
Well, after a bunch of tests I could made it work. The point was that the phone must contain the full country code and the area code. So for example 00_52_55_4567_8932 877 or +52_55_4567_8932 both will work. But 55_4567_8932 and 4567_8932 will not work. I hope this can help someone else in the future. Thank you all!

Always display a certain number of digits

I am creating a calculator app. For aesthetic reasons (numbers need to line up with an image), the result always needs to be five digits long, even if the first four digits are spaces or zeros. For example, the user enters 1+1, the result is 2, but my app should display it as "00002" or " 2".
How can I achieve this?
Well, you contradict yourself when you say the number should always be five digits long then say your app should display it as "00002" or "2". I'll assume you actually want the former? Here's a simple example assuming your number is int x.
int x = 2;
NSString *string = [NSString stringWithFormat:#"%05d", x];

How are integers stored in iOS, in particular in .pList and CoreData [duplicate]

This question already has an answer here:
Integers not properly returned from a property list (plist) array in Objective-C
(1 answer)
Closed 8 years ago.
I am trying to take a small integer (eg 4) from a pList and put it into a managed object for later manipulation. However, by the time I come to take it out of the managed object and put it into an NSInteger it has changed completely. 4 has become 237371328 !
The number is stored as "Number" in the pList and integer 16 in the managed object.
I have two fields: timesAsNumber which is integer 16 and timesUsed which is string (my current work around!).
The lines of code involved are:
NSArray *usageFetchResults = [self.objectContext executeFetchRequest:request error:&error];
NSLog(#"Here is the usageArray: %#, with error: %#",usageFetchResults, error);
This gives the log result:
data: {\n feature = video;\n timesAsNumber = 4;\n timesUsed = 4;\n})"
), with error: (null)
So the logger knows the value of timesAsNumber is 4.
NSLog(#"timesAsNumber straight from the Managed Object: %#", [currentUseData valueForKey:#"timesAsNumber"]);
Produces result: timesAsNumber straight from the Managed Object: 4 so still no problem.
However,
NSInteger timesUsedAsInt = [currentUseData valueForKey:#"timesAsNumber"];
NSLog(#"times As Number now reads: %ld", (long)timesUsedAsInt);
Produces the result: times As Number now reads: 237371328
I need to have the number as an integer for manipulation and my workaround of storing as string and converting to and fro is hardly elegant!
I teach High School computing so I know about storage of floating point numbers: excess-127, twos complement etc. I assume the problem arises from the different ways the integer is stored in the pList, the managed object and the NSInteger. However, I cannot figure out what those storage methods are so that I can work with them.
Any help gratefully received.
Tim.
When you are storing a number in plist or coreData, it is stored as NSNumber. So you access the value as NSInteger myInt = myNumber.intValue. When you are converting it back, use NSNumber *myNumber = [NSNumber numberWithInt:myInt]. That large number (237371328) could appear because you forgot to convert the NSNumber to int. Hope this helps.
Edit:
Try:
NSInteger timesUsedAsInt = [[currentUseData valueForKey:#"timesAsNumber"] intValue];
NSLog(#"times As Number now reads: %ld", (long)timesUsedAsInt);

iOS: Find out number of fraction digits [duplicate]

This question already has answers here:
digits after decimal point
(2 answers)
How to calculate number of digits after floating point in iOS?
(3 answers)
Closed 9 years ago.
I have some numbers that comes from server, how to know how many fraction digits in number ?
I mean how to know that 2.43 has 2 numbers after coma, 3.145 - 3 numbers, 2.0003 - 4 numbers.
Thanks in advance..
Assuming you are using a number, change it to a string. Remove the decimal point and get the last object and the length.
Example:
NSNumber *number = [NSNumber numberWithFloat:3.902];
[number.stringValue componentsSeparatedByString: #"."].lastObject.length;
I would do this :
NSNumber *number = [[NSNumber alloc] initWithFloat:3.902];
NSUInteger i = [[number stringValue] rangeOfString:#"."].location;
long numberOfDigits = [[number stringValue] length]-(i+1);
NSLog(#"%ld", numberOfDigits);
3
convert to string.
find the decimal point range.
subtract string length from the decimal point position and Add one.
Do you mean that you are retrieving the data from a server in a stream?
If your getting the data as a stream, it should be prepended with the length of the string, that is how you would know how many places to expect.
So on the server, first convert to string, then determine the length, or use another method suggested, then prepend the length of the string, and then send that number first.

Round floating value to .1 (tens place) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I round a float value to 2 post decimal positions?
Lets say I have a double number of 3.46.
How do I round it to 3.50?
I tried
NSLog(#"res: %.f", round(3.46));
but it return 3.
Do some calculations....
float f=3.46;
float num=f+0.05;//3.51
int intNum=num*10;//35
float floatNum=intNum/10.0;//3.5
NSLog(#"res: %.2f", floatNum); //3.50
Following code may help
i = roundf(10 * i) / 10.0;
where i is your float variable
If you're willing to live with the rounding rules from printf, then the following should suffice when rounding for presentation:
NSLog(#"res: %.1f0", 3.46);
Note that the 0 is just a normal character that is added after the value is formatted to the appropriate number (1) of decimal places. This approach should be usable with [NSString stringWithFormat:] as well.
The original code results in "3" because round always returns an integral value.
YMMV; I don't even use iOS.

Resources