Getting UnsafeBufferPointer from ObjC - objective-c

In my ObjC-Swift bridging I currently use
- (double*)vector {
return (double*)self.ndArray->vector();
}
- (long) size {
return self.ndArray->size();
}
to create an array in Swift like
let p = vector.vector()
let s = vector.size()
let a = Array<Double>(UnsafeBufferPointer(start: p, count: s))
Would it be possible to get this directly from ObjC like
let a = Array<Double>(vector.readyPackedPointer())
?

A C pointer does not contain any information about the size of the pointed-to memory region, so you'll always have to pass both pointer and count
from (Objective-)C to Swift. You could pass a struct containing both
pointer and count to save a function call.

Related

Obtaining a list of class names with objc_getClassList in JavaScript for Automation

By using ObjC.bindFunction in JXA, we can obtain an integer result from objc_getClassList.
Does anyone understand the types and bridging issues well enough to find a route to getting a list of class name strings returned by objc_getClassList into the JavaScript for Automation JSContext ?
(The code below returns only an [Object Ref] string)
(() => {
'use strict';
ObjC.import('stdlib');
ObjC.bindFunction('CFMakeCollectable', [ 'id', [ 'void *' ] ]);
ObjC.bindFunction('objc_getClassList', ['int', ['void *', 'int']]);
var classes = Ref();
const intClasses = $.objc_getClassList(null, 0);
$.objc_getClassList(classes, intClasses);
$.CFMakeCollectable(classes);
return [intClasses, classes];
//-> [11411, [object Ref]]
})();
The objc_getClassList function is expecting us to provide it with a buffer of memory to copy the class list into. Normally, JXA would treat the return type of malloc as a C array of unsigned chars, but using bindFunction we can cast malloc's return type to a C array of pointers, and make objc_getClassList's first argument match that type. Then, it's just a matter of indexing into the buffer (of type Ref) and passing that value into class_getName.
ObjC.bindFunction('objc_getClassList', ['int', ['void**', 'int']])
ObjC.bindFunction('malloc', ['void**', ['int']])
ObjC.bindFunction('class_getName', ['char *', ['void*']])
const numClasses = $.objc_getClassList(undefined, 0)
const classes = $.malloc(8*numClasses)
$.objc_getClassList(classes, numClasses)
for (i=0; i<numClasses; i++) {
console.log("classes[" + i + "]: " + $.class_getName(classes[i]))
}

How to convert Swift array into CFArray?

I'm trying to capture a window list in a Mac OS X app using Swift. The CGWindowListCreateImageFromArray function requires a CFArray. I've tried several things and this is the closest I've got. Or is there a better way to convert the array?
import Cocoa
// Example swift array of CGWindowID's
var windowIDs = [CGWindowID]();
windowIDs.append(1);
windowIDs.append(2);
// Convert to CFArray using CFArrayCreate
let allocator = kCFAllocatorDefault
let numValues = windowIDs.count as CFIndex
let callbacks: UnsafePointer<CFArrayCallBacks> = nil
var values: UnsafeMutablePointer<UnsafePointer<Void>> = nil
/* how do I convert windowIDs to UnsafeMutablePointer<UnsafePointer<Void>> for the values? */
let windowIDsCFArray = CFArrayCreate(allocator, values, numValues, callbacks);
let capture = CGWindowListCreateImageFromArray(CGRectInfinite, windowIDsCFArray, CGWindowImageOption(kCGWindowListOptionOnScreenOnly));
You can initialize your UnsafeMutablePointer with your array so long as you set your CGWindowIDs to CFTypeRef:
var windows: [CFTypeRef] = [1, 2]
var windowsPointer = UnsafeMutablePointer<UnsafePointer<Void>>(windows)
var cfArray = CFArrayCreate(nil, windowsPointer, windows.count, nil)
Converted Ian's answer to Swift 4:
let windows = [CGWindowID(17), CGWindowID(50), CGWindowID(59)]
let pointer = UnsafeMutablePointer<UnsafeRawPointer?>.allocate(capacity: windows.count)
for (index, window) in windows.enumerated() {
pointer[index] = UnsafeRawPointer(bitPattern: UInt(window))
}
let array: CFArray = CFArrayCreate(kCFAllocatorDefault, pointer, windows.count, nil)
let capture = CGImage(windowListFromArrayScreenBounds: CGRect.infinite, windowArray: array, imageOption: [])!
let image: NSImage = NSImage(cgImage: capture, size: NSSize.zero)
Swift.print(image)
Arrays in Swift are bridged to NSArray, given they contain objects, e.g., conform to [AnyObject] type. Since CGWindowID is a UInt32, you need to convert it to NS family, array's map() method is an elegant approach.
var windows: [CGWindowID] = [CGWindowID(1), CGWindowID(2)]
var array: CFArray = windows.map({NSNumber(unsignedInt: $0)}) as CFArray
This, however, doesn't reflect on the actual CGWindowListCreateImageFromArray problem. Here's the working solution for that:
let windows: [CGWindowID] = [CGWindowID(17), CGWindowID(50), CGWindowID(59)]
let pointer: UnsafeMutablePointer<UnsafePointer<Void>> = UnsafeMutablePointer<UnsafePointer<Void>>.alloc(windows.count)
for var i: Int = 0, n = windows.count; i < n; i++ {
pointer[i] = UnsafePointer<Void>(bitPattern: UInt(windows[i]))
}
let array: CFArray = CFArrayCreate(kCFAllocatorDefault, pointer, windows.count, nil)
let capture: CGImage = CGWindowListCreateImageFromArray(CGRectInfinite, array, CGWindowImageOption.Default)!
let image: NSImage = NSImage(CGImage: capture, size: NSZeroSize)
Swift.print(image) // <NSImage 0x7f83a3d16920 Size={1440, 900} Reps=("<NSCGImageSnapshotRep:0x7f83a3d2dea0 cgImage=<CGImage 0x7f83a3d16840>>")>
I'm not great at ObjC, please correct if wrong, but from what I understand by playing with the SonOfGrab example and particular piece of code below is that the final pointer structure contains window ids (UInt32) not inside the memory cell (memory property of UnsafePointer instance), but inside memory address (hashValue property).
const void *windowIDs[2];
windowIDs[0] = 10;
windowIDs[1] = 20;
It's interesting, since values aren't stored in the memory, but inside the address descriptors, with oldest architectures being 32-bit UInt32 values fit perfectly into address pointers. Perhaps back in the days when the memory was a limiting factor this made a lot of sense and was a great approach. Discovering this all night in Swift in 2016 made me suicidal.
What's worse it fails in Xcode 7.2 playground with certain window ids, probably because of the way it handles memory, but works in the actual app.

How do I access an enum in Swift from Objective-C?

There is a global enum defined in Objective-C:
typedef enum {
UMSocialSnsTypeNone = 0,
UMSocialSnsTypeQzone = 10,
UMSocialSnsTypeSina = 11, //sina weibo
} UMSocialSnsType;
This code sets the sharetype of a platform:
snsPlatform.shareToType = UMSocialSnsTypeDouban;
In Swift, I want to get the sharetype of the platform:
var snstype = snsPlatform!.shareToType
println(snstype)
Result: UMSocialSnsType (has 1 child)
snstype.toRaw()
Error: UMSocialSnsType does not have a member named "toRaw"
From what I can tell, UMSocialSNSType was declared in Objective-C without using the NS_ENUM macro, so it wasn't imported as a Swift enum. That means that instead of being able to use .toRaw() or UMSocialSNSType.Douban you have to use the different enumeration values as constant structs. Unfortunately the type also doesn't have the appropriate operators (== or ~=) set up, so you have to compare the value property.
var snstype = snsPlatform!.shareToType
switch snstype.value {
case UMSocialSnsTypeDouban.value:
println("douban")
case UMSocialSnsTypeEmail.value:
println("email")
default:
println("other")
}
if snstype.value == UMSocialSnsTypeDouban.value {
println("douban")
}
The good news is that it looks like all the constants autocomplete in Xcode, so you should be able to do find the comparisons you need to do that way.
It looks like the Swift-version of the bridged typedef...enum must be something like:
struct UMSocialSnsType {
var value:Int
init(_ val:Int) {
value = val
}
}
let UMSocialSnsTypeNone = UMSocialSnsType(0)
let UMSocialSnsTypeQzone = UMSocialSnsType(10)
let UMSocialSnsTypeSina = UMSocialSnsType(11)
// etc
Whereas if it had been declared in Objective-C with the NS_ENUM macro, it would look like:
enum UMSocialSnsType: Int {
case UMSocialSnsTypeNone = 0
case UMSocialSnsTypeQzone = 10, UMSocialSnsTypeSina // etc.
}

Dynamic allocation in Objective-C, returning pointers

I would like to make sure the value of the pointer myFunction() returns is available, when it's not an Obj-C object.
double * vectorComponents (); //Just an example
double * vectorComponents ()
{
double componentSet[] = {1, 2, 3};
return componentSet;
}
How can I dynamically allocate these variables an then how to dealloc them. If I don't do anything it won't work. Thanks everyone.
NSLog(#":)");
You can use the C standard library functions malloc() and free():
double *vectorComponents()
{
double *componentSet = malloc(sizeof(*componentSet) * 3);
componentSet[0] = 1;
componentSet[1] = 2;
componentSet[2] = 3;
return componentSet;
}
double *comps = vectorComponents();
// do something with them, then
free(comps);
(Documentation)
Also:
If I don't do anything it won't work.
Perhaps it's worth mentioning that it didn't work because it invokes undefined behavior. componentSet in your code was a local auto-array - it's invalidated at the end of its scope (i. e. it's deallocated at the time the function returns - exactly what you wanted not to happen.)
If you return a pointer that you dynamically allocate in the function then the caller will have ownership of the object and will be required to free the value.
/**
* Returns ownership, use free to release the value when done.
*/
double * vectorComponents()
{
double *componentSet = malloc(sizeof(double) * 3);
componentSet[0] = 1.0;
componentSet[1] = 2.0;
componentSet[2] = 3.0;
return componentSet;
}
void example()
{
double *components = vectorComponents();
//use components
free(components);
}
Given your example, first question is do you really need dynamic allocation? If you just want to return the address of an array initialized inside a function you can use a static variable:
double * vectorComponents ()
{
static double componentSet[] = {1, 2, 3};
return componentSet;
}
If you do need a dynamic array then there are many ways to do it. If you compute the array you can malloc() the storage to be free()'ed later. If you wish to initialize a dynamic array, then maybe change the values, and return it you can use a static array to do that. For example:
double * vectorComponents2 ()
{
static double componentSet[] = {1, 2, 3};
double *dynamic = malloc(sizeof(componentSet));
memcpy(dynamic, componentSet, sizeof(componentSet)); // copy values
// modify contents of dynamic here if needed
return dynamic;
}
Using memcpy and a static array is shorter than setting individual values and allows the contents and size of the array to be changed easily.

How to return a C-array from method in Objective-C?

I have a function that returns a variable and I want to know how to return an array the issue is it isn't an NSArray it is just an average C array like this...
-(b2Fixture*) addFixturesToBody:(b2Body*)body forShapeName:(NSString*)shape
{
BodyDef *so = [shapeObjects objectForKey:shape];
assert(so);
FixtureDef *fix = so->fixtures;
int count = -1;
b2Fixture *Fixi[4];
while(fix)
{
count++;
NSLog(#"count = %d",count);
Fixi[count]= body->CreateFixture(&fix->fixture);
if (Fixi[count]!=0) {
NSLog(#"Fixi %d is not 0",count);
}
if (body->CreateFixture(&fix->fixture)!=0) {
NSLog(#"body %d is not 0",count);
}
fix = fix->next;
}
return *Fixi;
}
If you see some variable types you don't know it's because I'm using cocos2d framework to make a game but I'm returning a variable of b2Fixture... This code compiles however only saves the value of the first block of the array "fixi[0]" not the whole array like I want to pass
anyhelp :) thankyou
You can't return a local array. You'll need to do some kind of dynamic allocation or pull a trick like having the array inside a structure.
Here is a link to an in-depth article that should help you out.
In general returning C arrays by value is a bad idea, as arrays can be very large. Objective-C arrays are by-reference types - they are dynamically allocated and a reference, which is small, is what is passed around. You can dynamically allocate C arrays as well, using one of the malloc family for allocation and free for deallocation.
You can pass C structures around by value, and this is common, as in general structures tend to be small (or smallish anyway).
Now in your case you are using a small array, it has just 4 elements. If you consider passing these 4 values around by value is reasonable and a good fit for your design then you can do so simply by embedding the C array in a C structure:
typedef struct
{
b2Fixture *elements[4];
} b2FixtureArray;
...
-(b2FixtureArray) addFixturesToBody:(b2Body*)body forShapeName:(NSString*)shape
{
BodyDef *so = [shapeObjects objectForKey:shape];
assert(so);
FixtureDef *fix = so->fixtures;
int count = -1;
b2FixtureArray Fixi;
while(fix)
{
count++;
NSLog(#"count = %d", count);
Fixi.elements[count]= body->CreateFixture(&fix->fixture);
if (Fixi.elements[count] != 0)
{
NSLog(#"Fixi %d is not 0",count);
}
if (body->CreateFixture(&fix->fixture) != 0)
{
NSLog(#"body %d is not 0", count);
}
fix = fix->next;
}
return Fixi;
}
...
// sample call outline
b2FixtureArray result = [self addFixturesToBody...]
Whether this standard C "trick" for passing arrays by value is appropriate for your case you'll have to decide.
Note: If b2fixture is an Objective-C object make sure you understand the memory management implications of having a C array of objects references depending on the memory management model (MRC, ARC, GC) you are using.
If you need to design function or method that has to return a fixed or limited size array, one possibility is to pass a pointer to the result array to the function or method as a parameter. Then the caller can take care of allocating space, or just use a local or instance variable array. You might want the called function to sanity check that the array parameter isn't NULL before using the array.