How to convert NSString "{265, 188}" to {265, 188} - objective-c

I have a NSString variable that contains "{265, 188}". I want just {265, 188} in a variable. What should I do?
Edit:
The code so far is:
//This I get from some XML so I change this part
NSDictionary* controlConditions =#{#"NSPosition":#"{265, 188}"};
NSString* a=(NSString*)[controlConditions valueForKey:#"AXPosition"];
After all, I need to send this to apple script set _controlid_ to a(variable) where i require it to be {265, 188}
Edit:
The variable controlConditions is taken from an XML that gives the position of a UIelement. The NSDictionary returns "{265, 188}" in a(variable). I need to send {265, 188} to an Applescript to match it to the position of various UIelements to get the right one. Hope this makes the problem clearer.

NSString *value = #"{265, 188}";
CGFloat x, y;
sscanf([value UTF8String], "{%lf, %lf}", &x, &y);
NSPoint point = NSMakePoint(x,y);
NSLog(#"%#", NSStringFromPoint(point));

So why don't you just put the values into a fixed array:
int anIntArray[2] = {265,188};
NSLog(#"anIntArray = %d, %d", anIntArray[0], anIntArray[1]);
prints:
anIntArray = 265, 188
Maybe you want to use some type other than int.

The question is really not clear. However, a string of the form {265, 188} is very possibly the result of a previous call to NSStringFromPoint() on an NSPoint whose x field is 265 and y field is 188.
Do you perhaps want to get the NSPoint value back out of the string? If so, you would pass the string to NSPointFromString().
That would not explain how quote characters actually got into the original string, if they are really there. (Unexpectedly, NSPointFromString() actually does still work with a string which contains quote characters.)

Related

How to set value of NSString into NSArray

I want to know how to set a value of NSString into NSArray
NSString *holdTheNumberToUpload;
NSArray *resultFetch;
self.resultFetch.count = holdTheNumberToUpload
I used this way:
if (!self.resultFetch.count) {
[self.defaults setObject: [NSString stringWithFormat:#"%lu", (unsigned long)self.resultFetch.count] forKey:#"holdTheNumberToUpload"];
}
You need to use an NSMutableArray instead of a NSArray. It would look like this:
NSString *holdTheNumberToUpload = #"Whatever";
NSMutableArray *resultFetch = [NSMutableArray array];
[resultFetch addObject:holdTheNumberToUpload];
Also, I'm not sure how you're using resultFetch.count. This value is an NSUInteger, so your check if (!resultFetch.count) will never be work because it will always be a non-negative number.
The words you use in your question don't seem to have anything to do with the screen shot. The problem with the code in the screen shot is the expression
[(unsigned long)self.resultFetch.count]
What is that supposed to be? If it's supposed to a number, remove the square brackets. If it's supposed to be an array, put # in front of it:
#[(unsigned long)self.resultFetch.count]
But that won't compile either, because (unsigned long)self.resultFetch.count is not an object. Therefore it cannot be an element of an array, nor can it be the parameter of setObject. Either way, you need to wrap it in an NSNumber, which is an object. Again, you can do this with #:
#self.resultFetch.count
or, if you really insist on the cast to unsigned long,
#((unsigned long)self.resultFetch.count)
So, depending what on earth your code is supposed to mean, you might end up with something like (you want a number?):
#self.resultFetch.count
or something like (you want an array containing a number?):
#[#self.resultFetch.count]

How to iterate through a string and find a certain character?

I want to go through what the user input. The user is going to input some 'useless' information that i don't need right now. How would i get the slice the string so I can get the part I want.
for example:
NSString *findingText = [prefs stringForKey:userSearching];
NSString *substring = [string substringFromIndex:[string length] + 4];
But this is where i have put a specific character that i would like to search for which is !. The user has entered some information that I have used as the key. So when the user wants to find the info all they have to do is search for it and will find the special character and use that to get the NSUserDefaults. but what the User Enters doesn't always have the same length. so was wondering how I could do this.
Thanks in advance.
You can use rangeOfString function
NSRagne range = [string rangeOfString:#"!"];
This will give you an NSRange struct that gives you the position of the ! character.
NSUInteger pos = range.location;

Objective-C put string at location of another string

I wan't to "know" how to put a string at a location in another string. I already know because I figured out another way to do this. But I wan't to know the real way, does it even exist?
I'm also asking this question for future questions on how to put this string at a location in another string the "false" way (in case it can't be done the real way)
What I mean about putting a (sub)string at a location of string is for example to put
this string:#"Hello" at location:5 inString:#"123456789"
I want the results to be:#"12345Hello6789"
Can this be done the real way? something like this fake code:
[str stringByPuttingString:#"s" atLocation:5];//this code does not exist
I figured out other ways to get this done, can we get it to shorter code?
-(NSString *)putString:(NSString *)str atLocation:(int)location ofString:(NSString *)mainString {
NSRange range = NSMakeRange(location, 0);
return [mainString stringByReplacingCharactersInRange:range withString:str];
}
and
-(NSString *)putString:(NSString *)str atLocation:(int)location ofString:(NSString *)mainString {
NSString *first = [mainString substringToIndex:location];
NSString *last = [mainString substringFromIndex:location];
return [NSString stringWithFormat:#"%#%#%#", first, str, last];
}
The first one feels best, any other ideas or real ideas?
Jonathan,
in future cases of this "problem".
Why not just use an NSMutableString?
NSMutableString *string = [NSMutableString stringWithString:#"123456789"];
[string insertString:#"Hello" atIndex:5];
NSLog(#"%#", string);
Outputs:
12345Hello6789
You can use NSMutableString to accomplish this task. Specifically, see the reference to the insertString:atIndex: method which will do exactly what you want, i.e. insert a string into another string at a specified location. API LINK
you can implement this by using NSMutableString method insertString:atIndex:
Inserts into the receiver the characters of a given string at a given location.
- (void)insertString:(NSString *)aString atIndex:(NSUInteger)anIndex
Parameters
aString
The string to insert into the receiver. aString must not be nil.
anIndex
The location at which aString is inserted. The location must not exceed the bounds of the receiver.
Taken from apple developer classes ref

How do I get a float value from a text field?

I've seen others ask similar questions, but none of the answers have worked for me; I get the message "Expected ';' at end of declaration list". Can someone walk me through what I'm supposed to do? (This is my first question here, so I'm sorry if I did something wrong.)
float yourVariable = [yourTextField.text floatValue];
Make sure you add validations to your textfield. If you have any non-numeric it will return 0.
How to convert NSString value #"3.45" into float?
I also gone through following link which says returns 0.0 if the receiver doesn’t begin with a valid text representation of a floating-point number.
float floatsample = [sampletextfield.text floatValue];
Do this:
NSNumberFormatter *floatFormatter = [[NSNumberFormatter alloc] init];
[floatFormatter numberFromString:textField.text];
Assuming textField is your textField. Then you can check floatFormatter for a nil NSNumber if it can't parse the string properly.

need help converting an MPMediaItemPropertyPersistentID to a string and back again

I'm getting the ID of a media item from the MPMediaPickerController. According to the documentation, this value is an NSNumber object containing a uint64_t (unsigned long long). I would like to convert it to an NSString for saving in my data model. However, when I convert it to a string its value changes, and when I convert it back to a number it changes again! Obviously I'm not understanding something about these data types. Can you help?
MPMediaItem *mediaPicked;
// set mediaPicked to an MPMedia item using MPMediaPickerController...
NSLog(#"id as number: %qu", [mediaPicked valueForProperty:MPMediaItemPropertyPersistentID]); // outputs 566042331449280
NSLog(#"id as string: %#", [[mediaPicked valueForProperty:MPMediaItemPropertyPersistentID] stringValue]); // outputs 16204893883745507648
NSLog(#"id as number: %qu", [[[mediaPicked valueForProperty:MPMediaItemPropertyPersistentID] stringValue] longLongValue]); // outputs 9223372036854775807
If I try to play the media item before this conversion, it always works. But if I try to play the media item after this conversion, only about half of the media items I've tried work. So some ID values survive the conversion and some don't.
You are starting by saying this:
NSLog(#"id as number: %qu", [mediaPicked valueForProperty:MPMediaItemPropertyPersistentID]); // outputs 566042331449280
But that's wrong. %qu means "this thing is an unsigned long long". But this thing is not an unsigned long long. It's an object! It's an NSNumber wrapped around an unsigned long long. You are lying to NSLog, so you're getting garbage output in your very first statement.
Now, try this on your own machine:
uint64_t x = 16204893883745507648ULL;
NSLog(#"%qu", x);
NSNumber* n = [NSNumber numberWithUnsignedLongLong:x];
NSLog(#"%#", n);
NSLog(#"%#", [n stringValue]);
All of those NSLog statements give the same result - because they are all correct formulations, unlike the one you started with. So, those NSLog statements show you the kind of thing you ought to be saying.
Now, you might think: Oh, great, so I can get from an NSNumber to an NSString with stringValue after all. Yes, but you can't get back again. We cannot get from [n stringValue] to a correct NSNumber by using longlongValue, because a long long is not an unsigned long long. There is no unsignedLonglongValue. So you can't get there from here.
So what's the right thing to do? Don't convert at all! You've got an NSNumber, it's valid, just keep it and use it. An NSNumber is a value you can store in your model. (For example, it can go into a dictionary as a value or as a key, it can be a value in user defaults, and so on.)