objective-c....deleting number number from my label by button - objective-c

i want to delete numbers individually by this button from my label
NSMutableString *str=(NSMutableString *)label.text;
str=[str replaceCharacterInRange:NSMakeRange([str length]-1,1) withString:#""];
error....."Void value not ignored as it ought to be"

You can't just cast an NSString as an NSMutableString and expect it to be mutable. You need to create a mutable string before you alter it.
NSMutableString *mutableString = [label.text mutableCopy];
[mutableString replaceCharactersInRange:NSMakeRange([mutableString length] - 1, 1) withString:#""];

replaceCharacterInRange:withString: returns void since it is a mutable operation that modifies the string.
To fix your problem, the first thing you need to know is that you can not make a string mutable just by casting it as NSMutableString you need to use mutableCopy.
NSMutableString *str= [label.text mutableCopy];
//Now the next thing do not assign str
[str replaceCharacterInRange:NSMakeRange([str length]-1,1) withString:#""];
...
//And finally when you are done if you are not using ARC
///then you need to release the string since you called `mutableCopy`.
[str release];

use deleteCharactersInRange:
[str deleteCharacterInRange:NSMakeRange([str length]-1,1) ])]

Related

Check if it is possible to break a string into chunks?

I have this code who chunks a string existing inside a NSString into a NSMutableArray:
NSString *string = #"one/two/tree";
NSMutableArray *parts = [[string componentsSeparatedByString:#"/"] mutableCopy];
NSLog(#"%#-%#-%#",parts[0],parts[1],parts[2]);
This command works perfectly but if the NSString is not obeying this pattern (not have the symbol '/' within the string), the app will crash.
How can I check if it is possible to break the NSString, preventing the app does not crash?
Just check parts.count if you don't have / in your string (or only one), you won't get three elements.
NSString *string = #"one/two/tree";
NSMutableArray *parts = [[string componentsSeparatedByString:#"/"] mutableCopy];
if(parts.count >= 3) {
NSLog(#"%#-%#-%#",parts[0],parts[1],parts[2]);
}
else {
NSLog(#"Not found");
}
From the docs:
If list has no separators—for example, "Karin"—the array contains the string itself, in this case { #"Karin" }.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/componentsSeparatedByString:
You might be better off using the "opposite" function to put it back together...
NSString *string = #"one/two/three";
NSArray *parts = [string componentsSeparatedByString:#"/"];
NSString *newString = [parts componentsJoinedByString:#"-"];
// newString = #"one-two-three"
This will take the original string. Split it apart and then put it back together no matter how many parts there are.

NSMutableString appendString in first position

I have a NSMutableString variable that i append from aString, like this:
NSString *aString = [NSString stringWithFormat:#"{\"MyCode\":%#,\"TotalAmount\":%#,\"PaymentType\":\"%#\",\"BankName\":\"%#\",\"BankAccountNo\":\"%#\",\"Amount\":\"%#\",\"FileName\":\"%#\"}",aCode,total,type,labelBank.txt,labelAcc.txt,aTrasfer,imageName];
[teststring appendString:asstring2];
[teststring appendString:#","];
in this code i sucess to append the string in order they append. But know i want to append a new string in the first position, just like in array object at index 0.Can NSMutableString do this?
Hope my question is clear..thanks
You said it, with an index. Same way as you do with an array.
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)anIndex
Your code:
[testString appendString:aString];
[testString insertString:#"," atIndex:0]; /* prepend the comma
Check out the Documentation

Change value of mutable string

How to change value of mutable string ? Here is what I do
NSString *str = #"This is string";
NSMutableString *str = [NSMutableString stringWithFormat:#"%#", str];
str = #"New string" -> wrong incompatible pointer types assigning to NSMutableString from NSString
You only need to use NSMutableString if you want to change parts of the string in place (append, insert etc.), often for performance reasons.
If you want to assign new values to the string variable, you're fine with a good old NSString as your last line simple assigns a complete new string object to str:
You can use setString to replace the whole string:
NSString *str = #"This is string";
NSMutableString *mutableStr = [NSMutableString stringWithFormat:#"%#", str];
...
[mutableStr setString:#"a different non mutable string"];
As indicated in another answer, a non-mutable NSString may be enough for your purposes.
This is how you should initialize a NSMutableString:
NSMutableString *string = [[NSMutableString alloc]init];
You could use any other way specified in the docs. The way you are doing it, you are not creating any instance of the NSMutableString class. Then, if you want to add some string to it:
[string appendString:#"content"];

Going crazy with UITextField

I'm literally going crazy whit these six rows of code.
NB: nome and prezzo are 2 textFields
NSString *itemName = (NSString *) [rowVals objectForKey:#"name"];
NSString *itemPrice = (NSString *) [rowVals objectForKey:#"price"];
nome.text = itemName;
nome.userInteractionEnabled = YES;
prezzo.text = itemPrice;
prezzo.userInteractionEnabled = YES;
Don't know why when itemPrice is copied in one of those label, the program go in SIGABRT.
Instead if I try to read the content with an NSLog(#"%#",itemPrice); it return the exact value, so it means that is a valid NSString.
The only solution I found is passing through a NSNumber:
NSNumber *itemPrice = (NSNumber *) [rowVals objectForKey:#"price"];
prezzo.text = [[NSString alloc] initWithFormat:#"%#", itemPrice];
There is another way to use directly the NSString?
Probably the value in the #"price" field is NSNumber, and not an NSString. The NSLog method will still provide a correct result, since %# is used for any NSObject subclass, not just NSString.
How about this:
NSString *itemPrice = [[rowVal objectForKey:#"price"] stringValue];
prezzo.text = itemPrice;
The problem might be the object type returned by [rowVals objectForKey:#"price"]. When you place the (NSString *) cast before the method call, you're telling the compiler what type of object is returned, but not actually converting it into an NSString. The line you use below does convert from NSNumber (or whatever other object) to a string: [[NSString alloc] initWithFormat:#"%#", itemPrice]
You might be storing NSNumber's object in NSDictionary instead of NSString.
There could be 2 ways: one would be to convert NSNumber to NSString while adding it to dictionary or the other way would be to convert NSNumber to NSString while assigning it to "itemName".
you may do the conversion for second option like:
NSString *itemPrice = [[rowVals objectForKey:#"price"]stringValue];

How to set the text of a previously-created NSMutableString?

I have an NSMutableString called makeString. I want to create it at the beginning of my program without having to set its text. I then want to be able to set its text. I am currently using the following to create it.
NSMutableString *make2String = [[NSMutableString alloc] initWithString:#""];
I am then using the following to set its text value.
make2String = [NSString stringWithFormat:#"Gold.png"];
Is this ok to do or is there a better way to set an NSMutableString's text?
That is not ok, you are replacing your mutable string with an ordinary immutable string (and leaking the original mutable string in the process). You could do [NSMutableString stringWithFormat:#"Gold.png"] after releasing the old string if you wanted to go that route. Or you could use NSMutableString's setString: method to set the content.
But if you're not actually mutating the string and just assigning different strings, you don't need NSMutableString at all. Just do make2String = #"Gold.png"; and be done with it.
NSMutableString * aString = [NSMutableString alloc];
aString = [aString init];
[aString setString:#"yourText"];
[aString setString:#"yourNewText"];
[aString setString:#"yourNewNewText"];
//...
[aString release];
make2String = [NSMutableString stringWithFormat:#"Gold.png"];
FYI: This is how I allocate NSMutableStrings without setting text
NSMutableString *string = [[NSMutableString alloc] init];