NSFastEnumerationIterator.Element (aka Any) has no subscript members - ios

after updating xcode to 8 version, I got this error
searchArray = allArray.filter({$0["test"] as? String == findCode
let resultText: NSString = ($0["test"] as? String)
return (resultText.range(of: searchText, options: NSString.CompareOptions.caseInsensitive).location) != NSNotFound
}) as NSArray!
Any thoughts?

The ultimate source of your problem is this line:
var allArray : NSArray!
NSArray is an Objective-C class. You are trying to write Objective-C code in Swift. Don't. This is Swift. Use Swift types! Write Swift code!
This is presumably an array of something. Swift needs to know what that is. For example, if this is an array of dictionaries, than its type would be [[AnyHashable:Any]]. If you use Swift types, your code will just work, because Swift will know what the elements of your arrays are.

Related

Swift 3 : Ambiguous use of mutablecopy

When i migrated the code from swift 2.3 to 3.0, its throwing an error as below:
let dictionary = (self.testArray!.object(at: i) as AnyObject).mutableCopy()
How to resolve this issue.
Do not use mutableCopy in Swift. The var keyword makes objects mutable
var dictionary = self.testArray![i] as! [String:Any]
And don't use Foundation collection types (NSArray / NSDictionary) in Swift either.
Use native types.

Ambiguous use of 'mutableCopy()' Swift3

I tried to update Swift 3 and I got the following error :
Ambiguous use of 'mutableCopy()'
Before update to swift 3. It runs well.
Swift 2.3
NSUserDefaults.standardUserDefaults().objectForKey("listsavednews")?.mutableCopy() as! NSMutableArray
Swift 3.0
(UserDefaults.standard.object(forKey: "listsavednews")? as AnyObject).mutableCopy() as! NSMutableArray
I found that mutableCopy in Swift3 return Any that doesnt have method mutableCopy() so that it needs to cast to AnyObject.
Any helps thanks.
I dont know why I can't comment.
Thanks all, I'll be using :
UserDefaults.standard.mutableArrayValue(forKey: "listsavednews")
mutableCopy is an Objective-C method from NSObject. There's little reason to use it in Swift 3.
Since you are dealing with UserDefaults and mutableCopy, you must be dealing with either an array or dictionary. Or it could be a string.
The proper way to do this in Swift 3 is to use the proper UserDefaults method to get an array or dictionary. And assign the result to a var. That combination will give you a mutable array or mutable dictionary.
var someArray = UserDefaults.standard.array(forKey: "somekey")
or:
var someDictionary = UserDefaults.standard.dictionary(forKey: "somekey")
In the two above cases, you end up with an optional since there might not be any data for the given key. And you also get a non-specific array or dictionary which isn't ideal. It would be better to cast the result to the appropriate type.
Let's say you have an array of strings and you want an empty array if there is nothing currently in user defaults. You can then do:
var someArray = UserDefaults.standard.array(forKey: "somekey" as? [String]) ?? []
Adjust as necessary if the array contains something other than String.
If you actually have a dictionary, the code would be similar.
var someDictionary = UserDefaults.standard.dictionary(forKey: "somekey") as? [String:String] ?? [:]
If your original object is just a string, then you could do:
var someString = UserDefaults.standard.string(forKey: "somekey") ?? ""

iOS SWIFT - File Archive - Argument Type "[String?]" : does not conform to expected type'AnyObject'

I am trying to write into an Archive and getting this error .
My tech stack is : XCode 7.1 Beta and SWIFT .Appreciate if any of you could share the exact code to fix this issue. Thanks in advance.
Argument Type "[String?]" : does not conform to expected type'AnyObject'
#IBAction func saveArch(sender: AnyObject) {
var contactArray = [name.text, address.text, phone.text]
NSKeyedArchiver.archiveRootObject(contactArray,
toFile: dataFilePath!)
}
Thanks
Array is not of type AnyObject
You should try
NSKeyedArchiver.archiveRootObject(NSArray(array: contactArray),
toFile: dataFilePath!)
You are sending a swift [] object that does not conform to AnyObject since arrays and object are different things in swift.
NSArray cannot contains optionals
You also have a problem with optionals: one or all of your .text is of type String? (therefor it might be nil).
If you are positive none of this field is nil, you can use
var contactArray = [name.text!, address.text!, phone.text!]
Or change the declaration.
If you are not sure, you should do something like
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!)
This way you only add non nil elements to the contactArray
By the way, Xcode 7.1 is out. No need to use the beta anymore

Swift Array addObjects to NSMutableArray

I have a Swift Array and I want to add all the objects inside to NSMutableArray
let stringName: String = "Something"
let stringNameSeperated = Array(logoName)
let mutableStringName: NSMutableArray = NSMutableArray(array: stringNameSeperated)
How can I do that?
I'm not really sure what you wanted to archive, but if you just wanted a Swift.Character sequence array, here's a simpler way without even the Array() process: (Notice that the result was casted into String. If you want Character, you will have to modify the code a bit.)
let stringName: String = "Something"
let mutableStringName: NSMutableArray = NSMutableArray(array: map(stringName) { String($0) } )
And from my understanding, you no longer have to case between Swift Array and Objective-C Array, you can use them interchangeably. Also, you can pass a Swift Array to a parameter that expected to be NSArray without problem in Swift 1.x as well.

EXC_BAD_INSTRUCTION object array assign Swift

I have an array of Printable objects, but I need them Equatable and AnyObject compliant.
private(set) var items: [Printable] = []
class func withItems<T: AnyObject where T: Equatable, T: Printable>(items: [T], selectedItem: T? = nil) {
... instance init ...
instance.items = items
}
And it result on EXC_BAD_INSTRUCTION:
fatal error: array cannot be bridged from Objective-C
This is one try to this problems:
Generic function and attribute with Equatable and Printable as parameters in Swift
why?
A Swift Array must contain all one kind of object (e.g. all String or all Int). An Objective-C NSArray can contain many different kinds of objects (e.g. some NSStrings and some NSNumbers). Hence if you get that kind of array from Objective-C you can't magically assign it into a Swift array reference.
What I do in that situation is munge the array to make it acceptable to Swift. I don't know what the details are of what you're getting back from Objective-C; your actual strategy will depend on those details and what you want to do with the array. One approach is to assign / cast into a Swift array of AnyObject. Or you might decide to leave it as an NSArray and work with it entirely through NSArray methods.
Here's an example from my own code. arr is an NSArray that's a mixed bag of NSString and NSNull objects. I know none of the NSString objects are the empty string, so I substitute the empty string for all the NSNull objects, thus giving me an array of just strings, which Swift can deal with:
let arr2 = (arr as Array).map { $0 as? String ?? "" }
Now arr2 is a pure Swift [String] array.

Resources