How to convert Objective-C line to Swift? - ios

I have code in Objective C:
NSMutableArray* paths = [textView.textContainer.exclusionPaths mutableCopy];
and now when I try to convert it to Swift:
self.textView.textContainer.exclusionPaths(mutableCopy())
it gives me error:
Cannot call value of non-function type '[UIBezierPath]'
How can I convert it?
Also I tried:
self.textView.textContainer.exclusionPaths.mutableCopy()
but exclusionPaths does not have mutableCopy() method

Simply do:
var paths = textView.textContainer.exclusionPaths
This will give you a mutable copy of the original array of paths.

In Swift declare a mutable type just with the var keyword
var paths = textView.textContainer.exclusionPaths

Use Array(textView.textContainer.exclusionPaths).

Related

'NSMutableSet' is not implicitly convertible to 'Set<NSObject>'

I am using coreplot to plot graph in my app.
When i try to assign X-Axis label or major tick location it is throwing error though NSMutableSet is converted to Set of NSObject Type.
Here is how i have written my code:
xAxis.axisLabels = xAxisLabels as Set<NSObject>
xAxis.majorTickLocations = xMajorTickLocations as Set<NSObject>
The error thrown is as follows
'NSMutableSet' is not implicitly convertible to 'Set<NSObject>'; did you mean to use 'as' to explicitly convert
You should use native Swift types rather than Foundation collections when possible. This eliminates the need for casting.
var xAxisLabels = Set<CPTAxisLabel>()
var xMajorTickLocations = Set<NSNumber>()

UnsafeMutablePointer for array using in C library

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.

NSMutableArray cast to swift Array of custom type

I'd like to figure out how to specify or cast an NSMutableArray to a swift Array of a custom type. I currently have 2 files:
First file requires an NSMutableArray for its functionality (passed by reference, ability to remove particular objects with indices I don't know)
Second file uses a Swift array (better memory / throwaway array), with a custom type, which I declare using
let newArray: [CustomType]!
I need to pass the NSMutableArray in as a parameter to a function in the second file, which requires a [CustomType]. When simply calling it:
let newVC = UIViewController(array: mutableArray)
it keeps telling me 'CustomType' is not identical to 'AnyObject'. I've tried calling the function using mutableArray as [CustomType], which does not work either. How can I make the swift Array function accept my NSMutableArray?
This works for me:
var swiftArray = NSArray(array:mutableArray) as Array<CustomType>
swiftArray is then a Swift array of objects of CustomType. You can pass the array and iterate over it as you would expect.
What I needed was this:
let newVC = UIViewController(array: mutableArray as AnyObject as [CustomType])

How to create a mutable array of specific class type in Swift

I have a class called Bullet and I want to make a mutable array with this class type. Basically the desired outcome I want is, when a user touches the screen a new bullet is spawned. I know in Java you can use an ArrayList to achieve this by adding a new instance to the array list each time.
I'm not sure syntactically how I could achieve in Swift or if it is even possible.
This is the code I have for declaring and instance of the Bullet class but how do I make it so that it is a mutable array...
var bullet: Bullet = Bullet()
Thank you!
Its simple :
var bulletArray = [Bullet]() // this will declare and initialize the bulletArray
In the above line of code the type is inferred for the bulletArray.
If you explicitly want to specify the type then,
var bulletArray:[Bullet] = [Bullet]() // this will declare and initialize the bulletArray
If you just want to declare the array but not initialize then
var bulletArray:[Bullet]
If you just declare the array then you need to initialize it in init() before using it. Or else you can declare array as optional type. As shown below,
var bulletArray: [Bullet]?
HTH :)
To create a typed array...
var bulletArray: [Bullet] = []
A mutable array in Swift is an array with a mutable reference:
var bulletList = [Bullet]()
Now you can add Bullet objects at will...

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