-2

i'm making my own calculator and i came to the question.
Sorry for newbie question , but I didn't find it.
How can i append a NSInteger to another NSInteger in Objective-C;
for example:
5 + 5 = 55
6 + 4 + 3 = 643
etc.

Sergei Bynas
  • 71
  • 1
  • 4
  • if you doing calculator question is related to http://stackoverflow.com/questions/4892152/what-is-a-fast-c-or-objective-c-math-parser and you will find solution there – RolandasR Mar 29 '16 at 14:33

2 Answers2

3

You have to convert them to strings. Here's one way:

NSNumber *i1 = @6;
NSNumber *i2 = @4;
NSNumber *i3 = @3;

NSMutableString *str = [NSMutableString new];
[str appendString:[i1 stringValue]];
[str appendString:[i2 stringValue]];
[str appendString:[i3 stringValue]];

NSLog(@"result='%@", str);

However, having said all that, it's not clear to me why you are concatenating at all.

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • concatenating to do one NSInteger value and then doing some operation with that value. Any other ideas to make this ? I just don't know . – Sergei Bynas Mar 29 '16 at 14:38
  • @SergeiBynas Sorry I don't understand your question. – trojanfoe Mar 29 '16 at 14:39
  • 1
    @fpg1503 Why the downvote? It's ironic you are downvoted and ask for an explanation and then do exactly the same thing yourself. – trojanfoe Mar 29 '16 at 14:54
  • @trojanfoe You should convert it back to integers in the end, he wants a `NSInteger` not a `NSString`. (I had explained it but An error occurred during my comment submission, my bad) – fpg1503 Mar 29 '16 at 15:01
  • @trojanfoe by the way on which basis are you telling I'm the one who down voted? – fpg1503 Mar 29 '16 at 15:03
  • 1
    @fpg1503 It wasn't really clear what the OP wanted (see the last sentence in my answer). My answer does what he described he wanted, even though I didn't see the value of doing it. There is no indication in his question that he wants an `NSInteger` result. I saw a 1 point drop in your rep and took an educated guess it was you, although I see from your excellent upvote/downvote ratio that that is something you don't do often. – trojanfoe Mar 29 '16 at 16:02
2

If they are a single digit (as in a calculator) you can simply do:

NSInteger newNumber = (oldNumber * 10) + newDigit;

or in a method:

- (NSInteger)number:(NSInteger)currentNumber byAdding:(NSInteger)newDigit {
    //Assumes 0 <= newDigit <= 9
    return (currentNumber * 10) + newDigit;
}

If they have more than one digit you can make them into strings, concatenate them and convert back to integers or use simple arithmetic to find out the power of 10 you must multiply by.

EDIT: 6 + 4 + 3 Assuming a digit is provided at a time:

NSInteger result = [self number:[self number:6 byAdding:4] byAdding:3];

Purely arithmetic solution:

- (NSInteger)powerOfTenForNumber:(NSInteger)number {
    NSInteger result = 1;
    while (number > 0) {
        result *= 10;
        number /= 10;
    }
    return result;
}


- (NSInteger)number:(NSInteger)currentNumber byAdding:(NSInteger) newNumber {
    return (currentNumber * [self powerOfTenForNumber:newNumber]) + newNumber;
}
fpg1503
  • 7,492
  • 6
  • 29
  • 49