UnsafeMutablePointer for array using in C library - ios

I want to implement C library into my iOS project. I'm using swift language.
I have a function where the input parameter - where output values are stored - is ar usual C double array:
double ar[6];
///...
err = c_lib_func(ar);
If I initialize inside swift like var ar: [Double] xCode says I have to use
UnsafeMutablePointer. But inside the docs I haven't found how to initialize n-lenght array for UnsafeMutablePointer. I just can do something like this:
var ar : UnsafeMutablePointer<Double>. But I can understand how to initialize it as 6-length array. Please, help me.
If I'm using
ar = [Double]
err = c_lib_func(ar);
the xCode shows to me this error:
/Users/admin/Documents/projects/myApp/myApp/file.swift:46:46: Cannot
convert value of type '[Double]' to expected argument type
'UnsafeMutablePointer'

In Swift, [Double] is an array of double values which is not what you are after. If you want to initialize an UnsafeMutablePointer you can just use:
var ar = UnsafeMutablePointer<Double>.alloc(6)
Use ar.dealloc(6) to release the memory again.

Related

Cannot invoke 'sequence' with an argument list of type '([AnyObject])'

I have upgraded to Xcode 7-beta and it gives me this error: Cannot invoke 'sequence' with an argument list of type '([AnyObject])'. That error is in this line of code:
sprite.runAction(SKAction.sequence(actionarray as [AnyObject]))
I found that in swift 2 I must remove part of it and it must look like this:
sprite.runAction(SKAction.sequence(actionarray))
But actionarray in NSMutableArray and now it gives me this error: Cannot invoke 'sequence' with an argument list of type '(NSMutableArray)'
This is the content of NSMutableArray:
var actionarray:NSMutableArray = NSMutableArray()
actionarray.addObject(SKAction.moveTo(CGPointMake(self.frame.size.width/2, -sprite.size.height), duration: NSTimeInterval(duration)))
actionarray.addObject(SKAction.removeFromParent())
sprite.runAction(SKAction.sequence(actionarray))
It worked well in Xcode 6. What should I change there?
Thanks
Why do you use NSMutableArray in Swift code in the first place?
Try replacing with Swift array like this (compiles in Playground):
import Cocoa
import SpriteKit
let sprite = SKSpriteNode()
var actionarray: [SKAction] = []
actionarray.append(SKAction.moveTo(CGPointZero, duration: NSTimeInterval(1.0)))
actionarray.append(SKAction.removeFromParent())
sprite.runAction(SKAction.sequence(actionarray))
Try using this syntax:
SKAction.sequence(actionarray as AnyObject as [SKAction])

Error: Deployment Update target 8.3 NSMutableArray and addObjectsFromArray - swift

After updating the xcode and my device some functions are not running anymore.
see It:
var jsonUnico: NSMutableArray! = jsonResult["lista"] as? NSMutableArray
self.tableList.addObjectsFromArray(jsonUnico)
Error: Cannot invoke 'addObjectsFromArray' with an argument list of type '(NSMutableArray!)'
It was working yesterday before upgrading
note: the tablelist is an NSMutableArray
Swift 1.2 no longer implicitly converts between NSArray and Swift’s native array type – you need to explicitly cast from one to the other. Since addObjectsFromArray takes a Swift array, that means you need to convert it to [AnyObject].
Normally you’d get a more helpful error message: error: 'NSMutableArray' is not implicitly convertible to '[AnyObject]'; did you mean to use 'as' to explicitly convert?, with a offer to “fix-it”. But it looks like this isn’t happening because of your use of implicitly-unwrapped optional NSMutableArray!.
But… this isn’t such a bad thing, since using implicitly-unwrapped optionals like that when fetching values out of dictionaries is dangerous (if the entry is ever not there, your app will crash). An alternative is:
if let jsonUnico = jsonResult["lista"] as? NSMutableArray {
let tableList = NSMutableArray()
// Xcode will recommend adding the "as [AnyObject]"
tableList.addObjectsFromArray(jsonUnico as [AnyObject])
}
But since you’re already doing an as above it, you may as well combine them:
if let jsonUnico = jsonResult["lista"] as? [AnyObject] {
tableList.addObjectsFromArray(jsonUnico)
}

componentsSeparatedByString method in Swift

I had some troubles last night accessing the elements of an array created via the componentsSeparatedByStringMethod. My goal is to extract some information from an url content variable.
var arr = urlContent.componentsSeparatedByString("<span class=\"Some HTML class\">")
I printed arr to the log, it works perfectly
I'd like to access the second element of the array arr, in order to get the info I need (the information right after the span tag). I thought:
var info = arr[1]
would work, but it doesn't. I get an error message saying that the subscript method doesn't work for an object of type "AnyObject" or something similar. So the problem is arr is not an Array Object type but an AnyObject type. I tried to convert it to an array but it didn't work either.
Could anybody help through this little trouble? :)
In xcode 6.1.1 componentsSeperatedByString method returns [AnyObject]. You have to cast it to [String] like so:
if let arr = urlContent.componentsSeparatedByString("<span class=\"Some HTML class\">") as? [String] {
// arr is now [Sstring]
}
In xcode 6.3 beta this method returns [String] so cast is not needed.

Convert UnsafeMutablePointer<CLLocationCoordinate2D> to [CLLocationCoordinate2D] in Swift

I write an app in Swift and is bridging some Objective-C code. One of these classes has a method that looks like this: + (CLLocationCoordinate2D *)polylineWithEncodedString:(NSString *)encodedString;.
In Swift, it said this method returns a UnsafeMutablePointer<CLLocationCoordinate2D>. What I want is a Swift array of CLLocationCoordinate2D.
What obviously doesn't work, but I tried, is this:
let coordinates: [CLLocationCoordinate2D] = TheClass.polylineWithEncodedString(encodedString)
which will give me the following error:
'UnsafeMutablePointer<CLLocationCoordinate2D>' is not convertible to '[CLLocationCoordinate2D]'
Is it somehow possible to convert this UnsafeMutablePointer<CLLocationCoordinate2D> to a [CLLocationCoordinate2D]? Or should I take a different approach?
You can just use the memory property of the UnsafeMutablePointer, which is the data behind the pointer. But keep in mind that UnsafeMutablePointer < CLLocationCoordinate2D > will return one CLLocationCoordinate2D, not an array, just as declared in the obj c function.
var coordinate: CLLocationCoordinate2D = TheClass.polylineWithEncodedString(encodedString).memory

NSGliff for Swift Dictionary Key - Using uintptr_t?

I need to use an NSGlyph as the key in a Swift dictionary
var glyph:NSGlyph //set to a glyph
var glyphDict:[NSGlyph:CGPath] //Will contain a cache of glyphs previously converted to paths
var path = glyphDict[glyph]
but I get:
error: '[NSGlyph : CGPath]?' does not have a member named 'subscript'
So I guess Apple hasn't defined a subscript for NSGlyph?
I've found this code from Apple's VectorTextLayer Sample Code that successfully uses a CGGlyph as a key in a CFDictionary. How can I adapt this to work in a Swift dictionary?
CGPathRef path = (CGPathRef)CFDictionaryGetValue(glyphDict, (const void *)(uintptr_t)glyph);
I understand that code is wrapping the CGGlyph into a uintptr_t. How could I do this in Swift?
I think you've copied different code in your question. That error happens when the variable you're using as dictionary is an optional, so declared as:
var glyphDict:[NSGlyph:CGPath]?
To solve the issue, you can read from the dictionary using optional chaining:
var path = glyphDict?[glyph]
Note that path is an optional itself.

Resources