How to Go from Cmutablepointer<Cgfloat> to Cgfloat[] in Swift

How to go from CMutablePointer CGFloat to CGFloat[] in Swift

EDIT: Just wanted to clarify a few things after learning a bit more from the online documentation (PDF).

There are a few commonly used pointer types in Swift, here is how they map to C equivalents:

Pointers as Arguments

CConstVoidPointer     => const void *
CMutableVoidPointer => void *
CConstPointer<Type> => const Type *
CMutablePointer<Type> => Type *

Pointers as Return Types, Variables, and Arguments*

COpaquePointer      => void *
UnsafePointer<Type> => Type *

NOTE: Arguments follow this rule only when they are more than one pointer level deep, otherwise see above.

Pointers for Class Types

CConstPointer<Type>              => Type * const *
CMutablePointer<Type> => Type * __strong *
AutoreleasingUnsafePointer<Type> => Type **

Swift Pointers

When using the CConstPointer<Type> pointer in Swift, you may pass any one of these:

  • nil, which will be evaluated as a NULL pointer
  • A CConstPointer<Type> value
  • A CConstVoidPointer value
  • A CMutablePointer<Type> value
  • A CMutableVoidPointer
  • A AutoreleasingUnsafePointer<Type> value which will be converted to CConstPointer<Type> if necessary
  • A Type value passed by address (& operator)
  • A Type[] array

NOTE:CConstVoidPointer can take any of the above values as well.

When using the CMutablePointer<Type> pointer in Swift, you may pass any one of these:

  • nil, which will be evaluated as a NULL pointer
  • A CMutablePointer<Type> value
  • A Type value passed by address (& operator)
  • A Type[] array passed by address (& operator)

NOTE:CMutableVoidPointer can take any of the above in addition to CMUtableVoidPointer values.

So it would seem in your case that a CMutablePointer<CGFloat> could also be a pointer to an array of CGFloat values. Though I am not completely sure how to dereference that in Swift. (Perhaps the as operator?)

CMutablePointer - how to access it?

Since Swift beta 5, scrollViewWillEndDragging(_:withVelocity:targetContentOffset:) has taken an UnsafeMutablePointer instance as its last argument. When this method is called by your scroll view, your implementation can access the underlying Core Graphics point through the pointer's pointee property.

Note that the spelling of pointee used to be memory before Swift 3.

How to get back value from CMutablePointer?

You have to initialize the struct:

var itemInfo = LSItemInfoRecord(flags: 0, filetype: 0, creator: 0, `extension`: nil)

...

let status: OSStatus = LSCopyItemInfoForURL(url, kLSRequestAllFlags, &itemInfo)

Note that extension must be escaped because it's a keyword in Swift.

Edit

A complete example:

var itemInfo = LSItemInfoRecord(flags: 0, filetype: 0, creator: 0, `extension`: nil)

var path: CFStringRef = "/Applications/Safari.app"
var isFile: Boolean = 0

let url = CFURLCreateWithFileSystemPath(nil, path, CFURLPathStyle.CFURLPOSIXPathStyle, isFile)

let status: OSStatus = LSCopyItemInfoForURL(url, kLSRequestAllInfo, &itemInfo)

let statusString: String = SecCopyErrorMessageString(status, nil).takeUnretainedValue()

println("Status: \(status), \"\(statusString)\"")
println("Creator: \(itemInfo.creator)")
println("Flags: \(itemInfo.flags)")

prints

Status: 0, "No error."
Creator: 1936093801
Flags: 271599758

Swift Code optimization for finding nearest target cgfloat from an array

Just add in explicit type information:

func nearest(to x:  CGFloat) -> CGFloat {
let f: (CGFloat, CGFloat) -> Bool = { (lhs: CGFloat, rhs: CGFloat) in
abs(lhs - x) < abs(rhs - x)
}

let result: CGFloat? = self.min(by: f)
return result ?? CGFloat.zero
}

How would I be able to store values like '0x97' in an array in Swift?

Jeff is correct.

You cannot use the information[] syntax anymore.

arrayName:[Type] = [member1, member2, etc] 

is the correct way to initialize an array.

Actually, to be more complete with this answer:

internal let arrayName:[Type] = [member1, member2, etc] 

if you will not be modifying the array.

internal var arrayName:[Type] = [member1, member2, etc] 

if you will be modifying the array later.

I understand that internal is the default access, but I'm finding with Swift that I need to be careful about how Swift infers types; I now always specify a type in a variable declaration and this care just carries over to explicitly declaring the access.

How to call CGRectDivide in Swift?

Try this:

CGRectDivide(r, &mySlice, &myRemainder, 44.0, CGRectEdge.MaxYEdge)

How should I decide if my func should return optional or implicitly unwrapped optional?

It's probably too early to have strict best practices for swift, but here's my opinion. In general implicitly unwrapped optionals should be used for a few cases such as outlets, properties that are nil until initialization and return values from Objective-C functions, otherwise a regular optional should be used.

If you are creating a function that will return a value or nil, do the other programmers interacting with your code a favor and make the return value an optional.

How do you alloc/dealloc unsafe pointers in Swift?

This works, Swift is smart enough to know what to do with the & operator:

let color = UIColor.purpleColor()
var r:CGFloat, g:CGFloat, b:CGFloat, a:CGFloat = 0
color.getRed(&r, green: &g, blue: &b, alpha: &a)
CGContextSetRGBStrokeColor(c, r, g, b, a)

If you really want to do the alloc yourself, use your favorite flavor and construct the pointer like this:

let p = UnsafeMutablePointer<CGFloat>(calloc(1, UInt(sizeof(CGFloat))))
// later don't forget to free(p)


Related Topics



Leave a reply



Submit