Transform CBPeripheral's UUID to string format : - objective-c

I am trying to transform a CBPeripheral's UUID to string format:
CBPeripheral* peripheral;
NSString *pUuid = (__bridge NSString *)(CFUUIDCreateString(nil, peripheral.UUID));
but i got this error:
Incompatible pointer types passing retainable parameter of type 'NSString *' to a CF functions expecting 'CFUUIDRef' (aka 'const struct _CFUUID *') type

peripheral.UUID is deprecated as of OSX 10.9 per the Apple Documents
To accomplish what you are trying to do, you use:
[peripheral.identifier UUIDString]

Related

How to do objective c style assignments after renaming file to .mm

This code compiles in a .m file, but not in an .mm file:
CFDataRef nativeSocket = CFWriteStreamCopyProperty((CFWriteStreamRef)outputStream, kCFStreamPropertySocketNativeHandle);
CFSocketNativeHandle *sock = (CFSocketNativeHandle *)CFDataGetBytePtr(nativeSocket);
The error message is:
Cannot initialize a variable of type 'CFDataRef' (aka 'const __CFData *') with an rvalue of type 'CFTypeRef' (aka 'const void *')
If I change the type of nativeSocket to CFTypeRef the error message becomes:
Candidate function not viable: cannot convert argument of incomplete type 'CFTypeRef' (aka 'const void *') to 'CFDataRef' (aka 'const __CFData *')
Please tell me how to fix this. I can't seem to guess what to google.
Thanks!
You just need to cast the result from CFWriteStreamCopyProperty() to the proper type:
CFDataRef nativeSocket = (CFDataRef)CFWriteStreamCopyProperty((CFWriteStreamRef)outputStream, kCFStreamPropertySocketNativeHandle);
CFSocketNativeHandle *sock = (CFSocketNativeHandle *)CFDataGetBytePtr(nativeSocket);
It's probably safer to extract the native handle this way, though:
CFSocketNativeHandle sock;
CFDataGetBytes(nativeSocket, CFRangeMake(0, sizeof(sock)), (UInt8*)&sock);
That avoids any issue with the data object's byte pointer being misaligned. Also, it ensures that you don't try to deference the byte pointer implicitly (by doing *sock) after the data object has been released.

Incompatible types casting 'NSString *' to 'CTStringRef *'

I'm just trying to cast NSString* to CTStringRef*
NSString *foobar = #"foobar";
CFStringRef *tmp = (__bridge_retained CFStringRef*)foobar;
Can someone help with this error?
"Incompatible types casting 'NSString *' to 'CTStringRef *' (aka const struct __CFString **)with a __bridge_retained cast"
I've tried with simply __bridge and it don't work either. From the documentation, I think the _retained is the right type I need.
Thanks.
If you look closely at the error message you will see what your problem is. The hint is in this part -
__CFString **
Notice the two * - This means that you are trying to cast to a pointer to a pointer, or in other words a reference to a reference. CTStringRef is already a reference, as implied by the 'Ref' part of the name, so you don't need the * in (__bridge_retained CFStringRef*)
Your code should read
NSString *foobar = #"foobar";
CFStringRef tmp = (__bridge_retained CFStringRef)foobar;

Objective-C can't convert NSNumber to int

I have a simple code that will iterate through an array of integers but when I tried using a "for in" loop, it said I needed to have an object so I used an NSNumber but in order for my code to work, it had to be an int so after the loop, I had it convert the NSNumber to an int. It gave me another error saying I need to declare the variable _strong for it to work so I did that but now it gives me these errors: "Incompatible pointer to integer conversion sending 'NSNumber *_strong' to parameter of type 'int'" and "Implicit conversion of 'int' to 'NSNumber *' is disallowed with ARC" Here is the code that gives an error:
for(__strong NSNumber *i in factors) {
i = [i intValue];
You've already declared "i" as an Objective C object (specifically a NSNumber object).
If you want to do the conversion, declare a separate and different variable, e.g.
int j;
and set your integer to that, like:
j = [i intValue];

Format specifies type 'unsigned int' but the argument has type 'id'

NSString *val = [NSString stringWithFormat:#"%u",[settingsMgr performSelector:NSSelectorFromString([[gets objectAtIndex:indexPath.section] objectAtIndex:indexPath.row])]];
In the above code I am getting a warning where settingsMgr is object class and gets is NSMutableArray.
NSMutableArray can't hold standard types (int, float, BOOL, etc), so they're stored wrapped (in NSNumber mostly). Try changing %u to %#.

warning: Semantic Issue: Incompatible pointer types initializing 'char *' with an expression of type 'NSString *'

I am trying to do the following:
NSString *personDesc = [NSString stringWithFormat:#"Person named %#", person.name];
char *myArguments[] = { personDesc, NULL };
But it is producing this error:
warning: Semantic Issue: Incompatible pointer types initializing 'char *' with an expression of type 'NSString *'
The reason I am trying to convert the NSString into a char is because I am passing myArguments into AuthorizationExecuteWithPrivileges
e.g.
AuthorizationExecuteWithPrivileges(auth, tool, kAuthorizationFlagDefaults, myArguments, NULL);
Any ideas?
As matt said above, you need to convert the NSString to a char* before you can do what you want. Try something like:
NSStringEncoding stringEncoding = NSUTF8StringEncoding;
NSString *personDesc = [NSString stringWithFormat:#"Person named %#", person.name];
const char *cPersonDesc = [personDesc cStringUsingEncoding:stringEncoding];
char *myArguments[] = { cPersonDesc, NULL };
// auth and tool already exist
AuthorizationExecuteWithPrivileges(auth, tool, kAuthorizationFlagDefaults, myArguments, NULL);
It's right. NSString is not char*. It is NSString. They have nothing to do with one another.
Since you give no indication of what you're really trying to do, no further advice can be given. You could convert NSString to char* if you wanted to, e.g. with getCString:maxLength:encoding:. But why would you want to?