Swift arrays identity "Type does not conform to protocol 'AnyObject'" error
Date : March 29 2020, 07:55 AM
it helps some times I create playground and write this code: , According to The Swift Programing Language: var a = [1, 2, 3] as NSArray //NSArray is not a Struct
var b = a
a === b //true
var a = [1, 2, 3]
var b = a
a == b //true
|
Swift Array: [AnyObject] vs. [xxxxxxxxClass] and the method "append"
Date : March 29 2020, 07:55 AM
it should still fix some issue Your original version works for me ( screenshot) (only slightly modified for testing with my data). You shouldn't have to do this dance, something else is causing trouble. I suggest cleaning up your class a bit and take advantage of Swift 2 using guard, map and error. It will be easier to debug and will work more efficiently anyway. class ProductData: NSObject {
var title = ""
private init(dict: [String : AnyObject]){
if let t = dict["title"] as? String { self.title = t }
super.init()
}
class func getTheData(fromJSONPath JSONPath: String) -> [ProductData] {
do {
// safely unwrap and typecast the values else return empty array
guard let url = NSURL(string: JSONPath),
let JSONData = NSData(contentsOfURL: url),
let JSONArray = try NSJSONSerialization.JSONObjectWithData(JSONData, options: [])
as? [[String : AnyObject]] else { return [] }
return JSONArray.map() { ProductData(dict: $0) }
} catch {
// this `error` variable is created by the `catch` mechanism
print(error)
// return empty array if unkown failure
return []
}
}
}
let test = ProductData.getTheData(fromJSONPath: "http://localhost:5678/file/test.json")
|
iOS SWIFT - File Archive - Argument Type "[String?]" : does not conform to expected type'AnyObject'
Tag : ios , By : Jason Jennings
Date : March 29 2020, 07:55 AM
I wish this helpful for you I am trying to write into an Archive and getting this error . , Array is not of type AnyObject You should try NSKeyedArchiver.archiveRootObject(NSArray(array: contactArray),
toFile: dataFilePath!)
var contactArray = [name.text!, address.text!, phone.text!]
var contactArray = [String]()
for element in [name.text, address.text, phone.text] where element != nil {
array.append(element!)
}
NSKeyedArchiver.archiveRootObject(NSArray(array: contactArray),
toFile: dataFilePath!)
|
"Cannot assign value of type 'String' to type 'AnyObject?'", Swift 3, Xcode 8 beta 6
Tag : swift , By : inquiringmind
Date : March 29 2020, 07:55 AM
wish helps you In b6, String no longer magically bridges to NSString. String is not a class; it's a struct. You need to do the bridging by hand: dict["key"] = "value" as AnyObject
|
Type "Any" has no subscript members despite casting as AnyObject on Swift 3?
Tag : swift , By : user130518
Date : March 29 2020, 07:55 AM
should help you out It's because you're using more than one subscript operator, because presumably this is something like an array of arrays. But NSMutableArray's subscript operator returns Any. As a result, cellDescriptors[0] is Any. You try to use [11] on the result, but Any doesn't accept subscripts because it's Any, not a collection type. Casting to AnyObject doesn't help because AnyObject is also not a collection type.
|