I m having some issues here.. I am using the following code to generate the uuid in mine application.
- (NSString *)GetUUID
{
CFUUIDRef theUUID = CFUUIDCreate(NULL);
CFStringRef string = CFUUIDCreateString(NULL, theUUID);
CFRelease(theUUID);
return [(NSString *)string autorelease];
}
This code returns a NSString object back. Now I want to store the generated UUID as unsigned int. Any suggestions please how can i do it here. Thanks.
According to the Wikipedia page, UUIDs are 128 bits long. The largest int value you'll be able to work with is 64 bits. Therefore, I'd says you can't store it in an int because there simply isn't room.
Convert UUID to String using .UUIDString
Take string's .hash property.
Cast to int.
int numericFormUUID = (int)UUIDString.hash;
Note: collision is possible, as always, with hashing. Two users could end up with the same value here, whereas UUID is guaranteed unique.
Related
How would I convert a pid_t instance into a string instance? I know that a pid_t is nothing more than a signed integer, and yet I can't do something like:
# suppose appID is already an instance of pid_t
NSString *appStringID = [NSString stringWithFormat:#"%#", appID];
Why does this bring up an error?
As a signed integer, it's just a formatting issue with your NSString:
do this instead:
NSString *appStringID = [NSString stringWithFormat:#"%ld", (long)appID];
A format string of "%#" implies that you're passing another NSString object in, which an integer (a raw C type) is not.
I have a bunch of devices.
They each have a UUID that distinguishes them from each other.
Logically, this is the natural thing to key them by in a dictionary tracking them all, then.
However, the [device UUID] method passes back a CFUUIDRef.
First off, this isn't an object.
But hey, we can fix that.
[device_dictionary setObject:device for Key(__bridge id)[device uuid]];
No, wait, that's not a valid key: It doesn't implement the <NSCopying> protocol.
What's more, since I'm casting these CFUUIDRefs into objects on the fly, will the dictionary even realize when it's been passed the same CFUUIDRef twice? Or will the new object created on the fly by the cast not register as the same object?
Care to help me brainstorm on this? How would you key a dictionary with UUIDs, when they were available as non-objects?
The obvious solution would be to convert the returned CFUUIDRef into a string for use as the key in the dictionary.
You can convert the CFUUIDRef to a CFStringRef using CFUUIDCreateString() as follows:
CFStringRef uuidString = CFUUIDCreateString(kCFAllocatorDefault, [device uuid]);
You can then use toll-free bridging to convert the CFStringRef to an NSString for use in an NSDictionary.
If you need to convert the string representation of the UUID back to a CFUUIDRef you can use CFUUIDCreateFromString() as follows:
CFUUIDRef uuid = CFUUIDCreateFromString(kCFAllocatorDefault, uuidString);
There is also a wrapper for CFUUIDRef which implements NSCoding which can be found at https://gist.github.com/294023. However, this doesn't appear to be ARC compatible.
CFUUIDRef is a CFType, and you can use any CFType as keys in a CFDictionary:
CFMutableDictionaryRef d = CFDictionaryCreateMutable(
kCFAllocatorDefault,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFUUIDRef u1 = CFUUIDCreateFromString(kCFAllocatorDefault, CFSTR("68753A44-4D6F-1226-9C60-0050E4C00067"));
CFUUIDRef u2 = CFUUIDCreateFromString(kCFAllocatorDefault, CFSTR("68753A44-4D6F-1226-9C60-0050E4C00067"));
CFDictionarySetValue(d, u1, CFSTR("fnord"));
CFShow(CFDictionaryGetValue(d, u2)); // prints "fnord"
Unfortunately toll-free bridging doesn't work properly in this case, as -copy will be called on the key in NSMutableDictionary's -setObject:forKey: before the custom callbacks (see http://www.cocoabuilder.com/archive/cocoa/163407-using-nsimages-as-keys-to-dictionary.html#163439) resulting in a crash. In any case, it is trivial to use CFDictionary in lieu of NSDictionary.
Another option is NSMapTable if you want an Objective-C solution:
NSMapTable *t = NSCreateMapTable(NSObjectMapKeyCallBacks, NSObjectMapValueCallBacks, 0);
NSMapInsert(t, u1, CFSTR("fnord"));
NSLog(#"%#", NSMapGet(t, u2)); // prints "fnord"
i have integer data, stored in NSUserDefaults, there is my code:
- (IBAction)addButton:(id)sender {
NSInteger oldValue = [[NSUserDefaults standardUserDefaults] integerForKey:#"myValue"];
NSString *string1=[addTextField text];
int add = [string1 floatValue];
int new = globalCalories;
int old=oldValue;
if(recomended.text==[NSString stringWithFormat:#"%i", oldValue]){
**self.day = (int)roundf(old-add);
dayLeft.text=[NSString stringWithFormat:#"%d", oldValue-add];
}**
else
{
self.day=(int)roundf(new-add);
dayLeft.text=[NSString stringWithFormat:#"%d", day];
}
}
I copied all of button action code, just in case, but i did mark with bold strings of code, that appear to not work. So, it suppose to do mathematical operations with stored data (oldValue), but when i launch programs, it dosnt, in fact, it does, but instead of valid value program "think" that oldValue is 0 (instead of valid value).
So, when it contain, for example, number 2000, and I launch program and enter in text field 500, it suppose to be 1500 (2000-500), but it shows -500.
You can convert recommended.text to integer by using this:
int recommendedValue = [recommended.text intValue];
then compare the numbers.
The problem is that == compared the address es of the NSString and not their values (see many SO questions). To compare strings use the isEqualToString: method.
However in this case it would be even better to compare the numbers ie convert recomended.text to the number and use a intValue method on dayLeft.
How to convert a CGPDFStringRef to unicode char? I have used CGPDFStringCopyTextString to get the string and then [string characterAtIndex:i] to cast to unichar, is this the right way? or is there any way to get the bytes of the string and convert to unicode directly?
Need some guidance here.
NSString is capable of handling of unicode characters itself, you just need to convert the CGPDFString to NSString and further you can use it as follows:
NSString *tempStr = (NSString *)CGPDFStringCopyTextString(objectString);
although UPT's answer is correct, it will produce a memory leak
from the documentation:
CGPDFStringCopyTextString
"...You are responsible for releasing this object."
the correct way to do this would be:
CFStringRef _res = CGPDFStringCopyTextString(pdfString);
NSString *result = [NSString stringWithString:(__bridge NSString *)_res];
CFRelease(_res);
It's not a bad idea, even if you can access the CGPDFString directly using CGPDFStringGetBytePtr. You will also need CGPDFStringGetLength to get the string length, as it may not be null-terminated.
See the documentation for more info
I'm trying to extract a string (which contains an integer) from an array and then use it as an int in a function. I'm trying to convert it to a int using intValue.
Here's the code I've been trying.
NSArray *_returnedArguments = [serverOutput componentsSeparatedByString:#":"];
[_appDelegate loggedIn:usernameField.text:passwordField.text:(int)[[_returnedArguments objectAtIndex:2] intValue]];
I get this error:
passing argument 3 of 'loggedIn:::' makes pointer from integer
without a cast
What's wrong?
I really don't know what was so hard about this question, but I managed to do it this way:
[myStringContainingInt intValue];
It should be noted that you can also do:
myStringContainingInt.intValue;
You can just convert the string like that [str intValue] or [str integerValue]
integerValue
Returns the NSInteger value of the receiver’s text.
(NSInteger)integerValue
Return Value
The NSInteger value of the receiver’s text, assuming a decimal representation and skipping whitespace at the beginning of the string. Returns 0 if the receiver doesn’t begin with a valid decimal text representation of a number.
for more information refer here
NSArray *_returnedArguments = [serverOutput componentsSeparatedByString:#":"];
_returnedArguments is an array of NSStrings which the UITextField text property is expecting. No need to convert.
Syntax error:
[_appDelegate loggedIn:usernameField.text:passwordField.text:(int)[[_returnedArguments objectAtIndex:2] intValue]];
If your _appDelegate has a passwordField property, then you can set the text using the following
[[_appDelegate passwordField] setText:[_returnedArguments objectAtIndex:2]];
Basically, the third parameter in loggedIn should not be an integer, it should be an object of some kind, but we can't know for sure because you did not name the parameters in the method call. Provide the method signature so we can see for sure. Perhaps it takes an NSNumber or something.
Keep in mind that international users may be using a decimal separator other than . in which case values can get mixed up or just become nil when using intValue on a string.
For example, in the UK 1.23 is written 1,23, so the number 1.777 would be input by user as 1,777, which, as .intValue, will be 1777 not 1 (truncated).
I've made a macro that will convert input text to an NSNumber based on a locale argument which can be nil (if nil it uses device current locale).
#define stringToNumber(__string, __nullable_locale) (\
(^NSNumber *(void){\
NSLocale *__locale = __nullable_locale;\
if (!__locale) {\
__locale = [NSLocale currentLocale];\
}\
NSString *__string_copy = [__string stringByReplacingOccurrencesOfString:__locale.groupingSeparator withString:#""];\
__string_copy = [__string_copy stringByReplacingOccurrencesOfString:__locale.decimalSeparator withString:#"."];\
return #([__string_copy doubleValue]);\
})()\
)
If I understood you correctly, you need to convert your NSString to int? Try this peace of code:
NSString *stringWithNumberInside = [_returnedArguments objectAtIndex:2];
int number;
sscanf([stringWithNumberInside UTF8String], "%x", &flags);