NSGliff for Swift Dictionary Key - Using uintptr_t? - ios

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.

Related

How to convert Objective-C line to Swift?

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).

Swift - Array Filter not removing objects

I have an Array of Strings as shown:
I also have an array of objects that contain:
When I run the following line:
let filterNo = self.responseObjs.filter({!formItemIds.contains(String(describing: $0.formItemId))})
I expect filterNo to be empty as all the formItemIds are contained in the array. It is however not removing any of the items. Am I missing something basic?
Remove the describing from the init of String and use Nil-Coalescing Operator with $0.formItemId to unwrapped optional.
let filterNo = self.responseObjs.filter({!formItemIds.contains(String($0.formItemId ?? 0))})
You are not getting filtered data because your formItemId property is optional and using String(describing: $0.formItemId) give you output like Optional(98)

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.

Difference between declaring a variable in ios Swift?

I am learning Swift recently and I found that there are two types of declaration for a variable.
var indexArray = NSMutableArray() //and
var indexArray : NSMutableArray = NSMutableArray()
Am just wondering what is the difference between them? Will it replicate in any kind of assigning values to the variable?
Here is a simple explanation
var indexArray = NSMutableArray()
As the above, indexArray variable can be any one , String , Int , ....... You didn't specifically give any type for that variable.
var indexArray : NSMutableArray = NSMutableArray()
In here you specifically give that indexArray is a NSMutableArray
You can provide a type annotation when you declare a constant or variable, to be clear about the kind of values the constant or variable can store. Write a type annotation by placing a colon after the constant or variable name, followed by a space, followed by the name of the type to use.
This example provides a type annotation for a variable called welcomeMessage, to indicate that the variable can store String values:
var welcomeMessage: String
The colon in the declaration means “…of type…,” so the code above can be read as:
Declare a variable called welcomeMessage that is of type String.
The phrase “of type String” means “can store any String value.” Think of it as meaning “the type of thing” (or “the kind of thing”) that can be stored.
The welcomeMessage variable can now be set to any string value without error:
welcomeMessage = "Hello"
You can define multiple related variables of the same type on a single line, separated by commas, with a single type annotation after the final variable name:
var red, green, blue: Double”
* Note *
It is rare that you need to write type annotations in practice. If you provide an initial value for a constant or variable at the point that it is defined, Swift can almost always infer the type to be used for that constant or variable, as described in Type Safety and Type Inference. In the welcomeMessage example above, no initial value is provided, and so the type of the welcomeMessage variable is specified with a type annotation rather than being inferred from an initial value.
Excerpt From: Apple Inc. “The Swift Programming Language (Swift 2
Prerelease).” iBooks. https://itun.es/us/k5SW7.l
One of the important language feature of Swift is Type Inference. What it means is that a variable can identify what is its type based on the value its assigned with.
var indexArray = NSMutableArray()
By the above statement, it is implicitly known that the variable indexArray is of type NSMutableArray without being specified.
Whereas in the other type of declaration,
var indexArray : NSMutableArray = NSMutableArray()
you are explicitly specifying that the variable indexArray is of type NSMutableArray before assigning a value to it. If you assign a different type to it, the compiler is throw an error.
var indexArray : NSMutableArray = NSString() // This is wrong
A very good starting point is to go over the Swift Language document by Apple.
No difference, both are same..
there is difference between the follwing declarations,
var welcomeMessage: String? - can be nil or assigned nil at any point of time
var welcomeMessage: String! - can never be nil at any point of time when using that variable.
var welcomeMessage: String - will throw error saying, class has no initializers
But there is no difference between the following two,
var welcomeMessage: String = String()
var welcomeMessage = String()
In the first case, indexArray takes the type of the assignment source automatically, so in your example case the statements are exactly the same.
The difference besides the second version being more explicit is that you could tweak the type of indexArray - for example to a base class - in the second case;
var indexArray = NSMutableArray() // indexArray is type NSMutableArray
var indexArray : NSArray = NSMutableArray() // indexArray is type NSArray
// but contains an NSMutableArray
Of course, if you declare a variable without assigning it you can't take the type automatically from the assignment source, so then you need the explicit type declaration.

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)
}

Resources