How to change a sklabelnode into a nsinteger [duplicate] - ios

This question already has answers here:
Convert NSString to NSInteger?
(7 answers)
Closed 8 years ago.
How can i properly change the following into a NSInteger:
countDown.text = [NSString stringWithFormat:#"%i", (int)(currentTime-startTime)];
Countdown is a SKLabelnode

There's no real benefit to casting to an NSInteger (which is just a typedef for long on 64-bit architectures and int on 32-bit architectures) , but you can do that by changing your code into the following:
countDown.text = [NSString stringWithFormat:#"%ld", (NSInteger)(currentTime-startTime)];

You could use NSNumberFormatter
For example:
NSString *string = #"12";
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSInteger i = [[formatter numberFromString:string] integerValue];

Related

is there any way to add integers from an array without take elements to outside [duplicate]

This question already has answers here:
ios - How get the total sum of float from a NSMutableArray
(3 answers)
Closed 8 years ago.
I want to sum of currency from NSMutableArray. Ex: I have an arrayA (1,234.56 , 2,345.67) and after sum items in array, I want result show: 3,580.23 to put it on the Label. Is there the way to implement this?
Thanks
If the values are stored as NSNumber objects, you can use the collection operators. For example:
NSArray *array = #[#1234.56, #2345.67];
NSNumber *sum = [array valueForKeyPath:#"#sum.self"];
If you want to format that sum nicely using NSNumberFormatter:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSString *result = [formatter stringFromNumber:sum];
NSLog(#"result = %#", result);
If your values are really represented by strings, #"1,234.56", #"2,345.67", etc., then you might want to manually iterate through the array, converting them to numeric values using the NSNumberFormatter, adding them up as you go along:
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
NSArray *array = #[#"1,234.56", #"2,345.67"];
double sum = 0.0;
for (NSString *string in array) {
sum += [[formatter numberFromString:string] doubleValue];
}
NSString *result = [formatter stringFromNumber:#(sum)];
NSLog(#"result = %#", result);
The simplest way is this:
NSMutableArray *array = [NSMutableArray arrayWithArray:#[#(1234.56), #(2345.67)]];
double sum = [[array valueForKeyPath: #"#sum.self"] doubleValue];

How to convert NSString to NSMutableString in Objective-C? [duplicate]

This question already has answers here:
How to initialize NSString to NSMutableString?
(4 answers)
Closed 8 years ago.
Is there any way to convert an NSString to NSMutableString in Objective-C?
NSString *string = #"A string.";
NSMutableString *mutableString = [string mutableCopy];

Change numbers to text iOS [duplicate]

This question already has answers here:
How do I convert an integer to the corresponding words in objective-c?
(7 answers)
Closed 8 years ago.
I am not sure how to explain this but I need something like this and I'm not quite sure how to do it. I have a textfield where the user enters a number. For example "200". I got a label that must show "Two Hundred"(The number entered in Words )
Any ideas on how to do this?
Thanks in advance sorry for my bad english
try this:
//textField.text is 200
NSInteger someInt = [textField.text integerValue];
NSString *numberWord;
NSNumber *numberValue = [NSNumber numberWithInt:someInt];
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
numberWord = [numberFormatter stringFromNumber:numberValue];
NSLog(#"numberWord= %#", numberWord); // Answer: two hundred
yourLabel.text = numberWord;
You can use a NSNumberFormatter It can convert an NSNumber into its word representation.
NSNumber* number = #100;
NSString* textNumber;
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterSpellOutStyle];
textNumber = [numberFormatter stringFromNumber:number];
Here's something totally different. A bit more complex and weird, but you can fiddle around with it if you seek more manual control over the output. This is set up to serve a value up to 100,000 (to demonstrate a conditional set up).
Have fun:
int theInteger = 1,123;
int convert1000 = 0;
int convert100 = 0;
int convert10 = 0;
int convert1 = 0;
NSString * wordThousand = #"Thousand";
NSString * wordHundred = #"Hundred";
NSArray *wordsArraySingleDigit = [[NSArray alloc] initWithObjects:#"Zero",#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine",nil];
NSArray *wordsArrayDoubleDigit = [[NSArray alloc] initWithObjects:#"Ten",#"Eleven",#"Twelve",...,#"Ninety Eight", #"Ninety Nine",nil];
convert1000 = (theInteger - (theInteger % 1000))/1000;
convert100 = ((theInteger - (convert1000 * 1000)) - ((theInteger - (convert1000 * 1000)) % 100))/100;
convert10 = ((theInteger - (convert1000 *1000)-(convert100 *100)) - ((theInteger -(convert1000 *1000) - (convert100 *100)) % 10))/10;
convert1 = (theInteger - (convert1000 *1000)-(convert100 *100) - (convert10 *10));
if (theInteger > = 10000 && < 100000){
NSString * convertedThousand = [wordsArrayDoubleDigit objectAtIndex : convert1000];
convertedThousand = [NSString stringWithFormat: #“%# %#“,convertedThousand,wordThousand];
NSLog (convertedThousand);
}
if (theInteger >= 1000 && <= 10000){
NSString * convertedThousand = [wordsArraySingleDigit objectAtIndex : convert1000];
convertedThousand = [NSString stringWithFormat: #“%# %#“,convertedThousand,wordThousand];
NSLog (convertedThousand);
}
NSString * convertedHundred = [wordsArraySingleDigit objectAtIndex : convert100];
convertedHundred = [NSString stringWithFormat: #“%# %#“, convertedHundred,wordHundred];
NSLog (convertedHundred);
NSString * convertedTen = [wordsArraySingleDigit objectAtIndex : convert10];
convertedTen = [NSString stringWithFormat: #“%#“, convertedTen];
NSLog (convertedTen);
NSString *convertedOne = [wordsArraySingleDigit objectAtIndex : convert1];
NSLog (convertedOne);
Hope this helps or gives you a starting point for an alternative approach.
I think you should account for every possibility and make a set of IF statements, for example, if the first digit of the number is 3, write "three", if the number has 4 digits, display "thousands"... of course you can organize your code to make the process easy.

Extracting from the right of a string in objective C [duplicate]

This question already has answers here:
Get last 2 characters of a string?
(2 answers)
Closed 9 years ago.
This seems to be what I'm looking for but in reverse. I would like the string to extract from the right not from the left.
The example extracting from the left is given:
NSString *source = #"0123456789";
NSString *firstFour = [source substringToIndex:4];
Output: "0123"
I'm looking for a version of the below that works from the right (what is below doesn't work)
NSString *source = #"0123456789";
NSString *lastFour = [source substringToIndex:-4];
Output: "6789"
the [source substringFromIndex:6]; won't work because sometimes I will get an answer that is 000123456789 or 456789 or 6789. In all cases I just need the last 4 characters from the string so that I can convert it to a number.
there must be a better way than a bunch of if else statements?
As you are not sure, about the length of the string, so you must check it before extracting like this:
NSString *source = #"0123456789";
NSNumber *number;
if (source.length>=4) {
NSString *lastFour=[source substringFromIndex:source.length-4];
number=#([lastFour integerValue]); //and save it in a number, it can be int or NSInteger as per your need
}
NSLog(#"%#",number);
Also if you want a quick method that you need to call several times, create a category :
#implementation NSString (SubstringFromRight)
-(NSString *)substringFromRight:(NSUInteger)from{
if (self.length<from) {
return nil;
}
return [self substringFromIndex:self.length-from];
}
#end
And use it as :NSLog(#"%#",[source1 substringFromRight:4]);
NSString *source = #"0123456789";
NSString *newString = [source substringFromIndex:[source length] - 4];
NSLog(#"%#",newString);
replace
NSString *lastFour = [source substringToIndex:-4];
with
NSString *lastFour = [source substringFromIndex:[source length] - 4];
which returns you the last 4 characters of your original string string in lastFour string.
You can use the following code to get last 4 characters from your string.
NSString *last4Characters = [source substringFromIndex:(source.length - 4)];
NSLog(#"Last 4 Characters:%#",last4Characters);
last4Characters=nil;
Please let me know if any issue.

How do I convert NSInteger to NSString datatype?

How does one convert NSInteger to the NSString datatype?
I tried the following, where month is an NSInteger:
NSString *inStr = [NSString stringWithFormat:#"%d", [month intValue]];
NSIntegers are not objects, you cast them to long, in order to match the current 64-bit architectures' definition:
NSString *inStr = [NSString stringWithFormat: #"%ld", (long)month];
Obj-C way =):
NSString *inStr = [#(month) stringValue];
Modern Objective-C
An NSInteger has the method stringValue that can be used even with a literal
NSString *integerAsString1 = [#12 stringValue];
NSInteger number = 13;
NSString *integerAsString2 = [#(number) stringValue];
Very simple. Isn't it?
Swift
var integerAsString = String(integer)
%zd works for NSIntegers (%tu for NSUInteger) with no casts and no warnings on both 32-bit and 64-bit architectures. I have no idea why this is not the "recommended way".
NSString *string = [NSString stringWithFormat:#"%zd", month];
If you're interested in why this works see this question.
Easy way to do:
NSInteger value = x;
NSString *string = [#(value) stringValue];
Here the #(value) converts the given NSInteger to an NSNumber object for which you can call the required function, stringValue.
When compiling with support for arm64, this won't generate a warning:
[NSString stringWithFormat:#"%lu", (unsigned long)myNSUInteger];
You can also try:
NSInteger month = 1;
NSString *inStr = [NSString stringWithFormat: #"%ld", month];
The answer is given but think that for some situation this will be also interesting way to get string from NSInteger
NSInteger value = 12;
NSString * string = [NSString stringWithFormat:#"%0.0f", (float)value];
NSNumber may be good for you in this case.
NSString *inStr = [NSString stringWithFormat:#"%d",
[NSNumber numberWithInteger:[month intValue]]];

Resources