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

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

Related

Swift 2.2 breaks optionals/unwrapping optionals

Swift 2.2 has broken almost all my code. Even this simple string assigning to label doesn't work anymore:
cell.categoryName.text = peopleArray![indexPath.row]["Name"] as? String
The error says "Downcast from 'String?!' to 'String' only unwraps optionals, did you mean to use '!!'?"
What changes do I have to do now.
EDIT:
More Problems:
if (dict["data"]!["DataDetails"] as! NSArray).count == 0 {
}
Due to this I am getting a segmentation fault and the error shows this: warning: cast from 'String?!' to unrelated type 'NSArray' always fails
UPDATE:
I was using NSDictionaries, NSArrays in my classes that seems to cause the problem. Changing all the literals from Obj-C to swift made the code work properly.
So, I will also recommend other developers to prefer swift literals.
it seems there are some slight differences when using the swift types and the objective-c NS... types
eg
let dic:NSDictionary? = ["a":"a"]
let str:NSString? = dic!["a"] as? NSString
let dic2:Dictionary? = ["b":"b"]
let str2:String? = dic2!["b"] //dont need to do any casting, already optional
print(str)
print(str2)
prints
Optional(a)
Optional("b")
so depending on how your array / dictionary is defined, you might need different casting/unwrapping

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.

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

error: Generic parameter 'R.Generator.Element' cannot be bound to non-#objc protocol type 'AnyObject'

I am querying HealthKit and saving it to CoreData. I fetch the data in a separate class. In TableViewController I append the data to an array:
if NSUserDefaults.standardUserDefaults().boolForKey("weightSwitch") == true {
xAxisDatesArray.append(cdFetchWeight.queryCoreDataDate())
yAxisValuesArray.append(cdFetchWeight.queryCoreDataData())
and pass it at tableView.dequeueReusableCellWithIdentifier
myCell.xAxisDates = xAxisDatesArray[indexPath.row]
myCell.yAxisValues = yAxisValuesArray[indexPath.row]
In UITableViewCell I initialise the variables (yAxisValues, xAxisDates) and pass them into a charting library which takes the x and y values and plot a chart.
class TableViewCell: UITableViewCell, TKChartDelegate {
var xAxisDates = []
var yAxisValues = []
plot....
I need to get the min and max values of yAxisValues so that I can set the appropriate y-axis range to the data.
I have tried to get the min and max with the following code:
func rangeMinAndMax(){
let minYvalue = minElement(yAxisValues)
let maxYvalue = maxElement(yAxisValues)
}
But this generates the error: Generic parameter 'R.Generator.Element' cannot be bound to non-#objc protocol type 'AnyObject'
- Question: Why and how can I fix it?
Any help would be much appreciated !
The thing to do in a situation like this is to throw away all the misleading dross and boil it down to the simplest possible case:
let arr : [AnyObject] = [1,2,3]
let min = minElement(arr) // same error: "Generic parameter blah-de-blah..."
So, you see, minElement doesn't work on an array of AnyObject, and that's what the error is telling you. And the reason is obvious: an AnyObject is not a Comparable. There is no "minimum" for a bunch of AnyObject things; the entire concept of one AnyObject being "less than" another AnyObject is undefined. You need to cast your array down to an array of something that minElement can work on, namely a Comparable of some kind.
For example, in that code, I can fix the problem like this:
let arr : [AnyObject] = [1,2,3]
let min = minElement(arr as [Int])
That is the sort of thing you need to be doing. Of course, what you cast down to depends upon what these elements actually are. It looks to me as if will probably be Double and NSDate respectively, but that's just a guess; I don't know what's in your arrays. You do (presumably). Note that an NSDate is not a Comparable so you will have a bit more work to do with that one.

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