Getting ambiguous reference to member subscript - swift3

I am updating my code to swift3.0 but getting ambiguous refrence to member? What wrong i might be doing. Here is the method I am getting error in.
open class func parseJsonTenantList(_ list: [NSDictionary]?, strElementName: String, attrName1: String, attrNameValue2: String) -> [TenantRegister]
{
var renantList: [TenantRegister] = []
var key: String?
if let dict : [NSDictionary] = list {
var value: String?
for i in 0..<dict.count {
/// if attribute name doesn't match then it returns nil
if let s1: AnyObject = dict[i].value(forKey: attrName1)
{
key = s1 as? String
}
if let s2: AnyObject = dict[i].value(forKey: attrNameValue2)
{
value = s2 as? String
}
if (!(String.stringIsNilOrEmpty(value) && String.stringIsNilOrEmpty(key)))
{
let t: TenantRegister = TenantRegister()
t.name = key
t.tenantId = Guid(value!)
renantList.append(t)
}
}
}
return renantList
}

The issue is you are using NSDictionary, to solved your problem simply cast the list to Swift's native type [[String:Any]] and then use subscript with it instead of value(forKey:)
if let dict = list as? [[String:Any]] {
var value: String?
for i in 0..<dict.count {
/// if attribute name doesn't match then it returns nil
if let s1 = dict[i][attrName1] as? String
{
key = s1
}
if let s2 = dict[i][attrNameValue2] as? String
{
value = s2
}
if (!(String.stringIsNilOrEmpty(value) && String.stringIsNilOrEmpty(key)))
{
let t: TenantRegister = TenantRegister()
t.name = key
t.tenantId = Guid(value!)
renantList.append(t)
}
}
}
In Swift use native type Dictionary [:] and Array [] instead of NSDictionary and NSArray to overcome this type of issues.

Related

raw Value of optional type not unwrapped

I am trying to unwrap the raw value of an enum type in func toDictionary() , but I get an error.
How can I fix this?
enum ChatFeatureType: String {
case tenants
case leaseholders
case residents
}
class Chat {
var featureType: ChatFeatureType?
init(featureType: ChatFeatureType? = nil
self.featureType = featureType
}
//download data from firebase
init(dictionary : [String : Any]) {
featureType = ChatFeatureType(rawValue: dictionary["featureType"] as! String)!
}
func toDictionary() -> [String : Any] {
var someDict = [String : Any]()
// I get error on the line below: Value of optional type 'ChatFeatureType?' not unwrapped; did you mean to use '!' or '?'?
someDict["featureType"] = featureType.rawValue ?? ""
}
}
As featureType is an optional you have to add ? or ! as the error says
someDict["featureType"] = featureType?.rawValue ?? ""
But be aware that your code reliably crashes when you create an instance of Chat from a dictionary and the key does not exist because there is no case "".
Actually the purpose of an enum is that the value is always one of the cases. If you need an unspecified case add none or unknown or similar.
This is a safe version
enum ChatFeatureType: String {
case none, tenants, leaseholders, residents
}
class Chat {
var featureType: ChatFeatureType
init(featureType: ChatFeatureType = .none)
self.featureType = featureType
}
//download data from firebase
init(dictionary : [String : Any]) {
featureType = ChatFeatureType(rawValue: dictionary["featureType"] as? String) ?? .none
}
func toDictionary() -> [String : Any] {
var someDict = [String : Any]()
someDict["featureType"] = featureType.rawValue
return someDict
}
}

Can't get key value as String from metadata

I'm grabbing metadata from an MPMediaItem like this:
let url = item.value(forProperty:MPMediaItemPropertyAssetURL) as? NSURL
let asset = AVURLAsset(url: url! as URL, options: nil)
let metaArray = asset.metadata
for metadata in metaArray{
print("-----metadata:\(metadata)")
print("-----metadata.key:\(String(describing: metadata.key))")
}
However, when I get a block of metadata printed the "key" is printed as a numeric value instead of "pcst" as shown in the printout:
-----metadata:<AVMetadataItem: 0x1740153f0, identifier=itsk/pcst, keySpace=itsk, key class = __NSCFNumber, key=pcst, commonKey=(null), extendedLanguageTag=(null), dataType=com.apple.metadata.datatype.int8, time={INVALID}, duration={INVALID}, startDate=(null), extras={
dataLength = 1;
dataType = 21;
dataTypeNamespace = "com.apple.itunes";
}, value=1>
-----metadata.key:Optional(1885565812)
This is happening for all of the metadata/keys (there are 29 in this particular media item).
Also note that this line of code:
let realString = NSString(string: metadata.key! as! String)
causes this error:
Could not cast value of type '__NSCFNumber' (0x1b80dcdf0) to 'NSString' (0x1b80edae8).
How can I get the string value for the key ("pcst") ?
May be what you are looking for is identifier property of AVMetadataItem.
for metadata in metaArray{
print(metadata.identifier ?? "DefaultValue")
}
In case others want to see the code I'm using:
func returnKeyString(_ inVal: String)->String{
// expecting the metadata for "identifier" as input - returns key value
// eg "itsk/ldes" -> "ldes"
// or "id3/%00WFD" etc. -> "wfd"
var sFinal:String = ""
if (inVal.contains("/")){
sFinal = (inVal.components(separatedBy: "/")[1])
}
if sFinal.contains("%"){
sFinal = sFinal.components(separatedBy: "%")[1]
let index1 = sFinal.index(sFinal.startIndex, offsetBy: 2)
sFinal = sFinal.substring(from: index1)
}
return sFinal.lowercased()
}

Swift: Finding an Object Property via regex

Target: The following function shall iterate over an array of objects and check a specific property of all objects. This property is a string and shall be matched with a user input via regex. If there's a match the object shall be added to an array which will further be passed to another function.
Problem: I don't know how to set up regex in Swift 3. I'm rather new in Swift at all, so an easily understandable solution would be very helpful :)
How it currently looks like:
func searchItems() -> [Item] {
var matches: [Item] = []
if let input = readLine() {
for item in Storage.storage.items { //items is a list of objects
if let query = //regex with query and item.name goes here {
matches.append(item)
}
}
return matches
} else {
print("Please type in what you're looking for.")
return searchItems()
}
}
This is what Item looks like (snippet):
class Item: CustomStringConvertible {
var name: String = ""
var amount: Int = 0
var price: Float = 0.00
var tags: [String] = []
var description: String {
if self.amount > 0 {
return "\(self.name) (\(self.amount) pcs. in storage) - \(price) €"
} else {
return "\(self.name) (SOLD OUT!!!) - \(price) €"
}
}
init(name: String, price: Float, amount: Int = 0) {
self.name = name
self.price = price
self.amount = amount
}
}
extension Item: Equatable {
static func ==(lhs: Item, rhs: Item) -> Bool {
return lhs.name == rhs.name
}
}
Solved. I just edited this post to get a badge :D
For the purpose of letting the answer to be generic and clear, I will assume that the Item model is:
struct Item {
var email = ""
}
Consider that the output should be a filtered array of items that contains items with only valid email.
For such a functionality, you should use NSRegularExpression:
The NSRegularExpression class is used to represent and apply regular
expressions to Unicode strings. An instance of this class is an
immutable representation of a compiled regular expression pattern and
various option flags.
According to the following function:
func isMatches(_ regex: String, _ string: String) -> Bool {
do {
let regex = try NSRegularExpression(pattern: regex)
let matches = regex.matches(in: string, range: NSRange(location: 0, length: string.characters.count))
return matches.count != 0
} catch {
print("Something went wrong! Error: \(error.localizedDescription)")
}
return false
}
You can decide if the given string does matches the given regex.
Back to the example, consider that you have the following array of Item Model:
let items = [Item(email: "invalid email"),
Item(email: "email#email.com"),
Item(email: "Hello!"),
Item(email: "example#example.net")]
You can get the filtered array by using filter(_:) method:
Returns an array containing, in order, the elements of the sequence
that satisfy the given predicate.
as follows:
let emailRegex = "[A-Z0-9a-z._%+-]+#[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailItems = items.filter {
isMatches(emailRegex, $0.email)
}
print(emailItems) // [Item(email: "email#email.com"), Item(email: "example#example.net")]
Hope this helped.
You can do the same with filter function
let matches = Storage.storage.items.filter({ $0.yourStringPropertyHere == input })

Parse JSON string to Model Object type Array

I got Encrypted data from API hit by below method
URLSession.shared.dataTask(with: url!)
converted data into JSON but still it is encrypted
var json = try(JSONSerialization.jsonObject(with: data!, options: .allowFragments))
converted it into string
let arr:String = json as! String
decrypted it
let jsonText = arr.fromBase64()//extension method, given end of question
now it is in Json Formate as below (this is only 1 record, there are more than 1 records in Json string)
{
"CompanyAlt_Key": 1,
"Company_Name": "XYZ LTD",
"TableName": "CompanyList"
},
I have a model of same type
public class CompanyList {
public var companyAlt_Key : Int?
public var company_Name : String?
public var tableName : String?
}
here is fromBase64 method
func fromBase64() -> String {
let data = NSData.init(base64Encoded: self, options: []) ?? NSData()
return String(data: data as Data, encoding: String.Encoding.utf8) ?? ""
}
I am facing problem to get the Json String into an array of type CompanyList class
Help would be appreciate
You'll need to convert your jsonString to data first:
let jsonData = jsonString.data(using: .utf8)!
The convert the data to an array
let array = JSONSerialization.jsonObject(with: jsonData, options: nil) as? [[String: Any]]
Then iterate through the array…
let companies = array?.map {
return CompanyList(dictionary: $0)
}
Implement an init method for your CompanyList, passing in a dictionary for each record in your response…
public class CompanyList {
public var companyAlt_Key : Int?
public var company_Name : String?
public var tableName : String?
init(dictionary: [String: Any]) {
companyAlt_Key = dictionary["companyAlt_Key"] as? Int
company_Name = dictionary["company_Name"] as? String
tableName = dictionary["tableName"] as? String
}
}
You can also use this to validate the data. If the fields in your class are non-optional, you can use an optional init as follows…
public class CompanyList {
public var companyAlt_Key : Int
public var company_Name : String
public var tableName : String
init?(dictionary: [String: Any]) {
guard let companyAlt_Key = dictionary["companyAlt_Key"] as? Int,
let company_Name = dictionary["company_Name"] as? String,
let tableName = dictionary["tableName"] as? String else {
return nil
}
self.companyAlt_Key = companyAlt_Key
self.company_Name = company_Name
self.tableName = tableName
}
}
If you're using an optional init, use flatMap to ensure you don't have any optional elements in your array…
let companies = array?.flatMap {
return CompanyList(dictionary: $0)
}

How do I cast an Array<AnyObject> to an empty array variable?

Problem: I can't assign a string array (typed as 'AnyObject') to an empty array.
Steps:
1. Get a dictionary containing strings & arrays.
2. Grab an array from this dictionary via the key 'photos'.
Note: Xcode's warning suggest that I give the explicit type 'AnyObject'.
3. Create an empty array.
4. Attempt to assign it (failed).
let responseDictionary = responseDict as [String : AnyObject]
let ricPhotos:AnyObject = responseDictionary["photos"]!
var thePhotos:Array<AnyObject>?
thePhotos = ricPhotos <--- fails
Compiler Error:
...'AnyObject' is not convertible to 'Array<AnyObject>'
Question: How do I assign 'ricPhotos' to the empty array 'thePhotos' and preferably cast 'AnyObject' to 'String'?
Revision
let responseDictionary = responseDict as [String : AnyObject]
var anyObject: AnyObject? = responseDictionary["photos"]
Okay, 'anyObject' appears to be a Dictionary, and inside it is 'photo' which is an array; as seen in the data.
Here's some of the data(anyObject):
{
page = 1;
pages = 1334;
perpage = 100;
photo = (
{
farm = 3;
"height_m" = 336;
"height_s" = 161;
"height_sq" = 75;
"height_t" = 67;
id = 15166756998;
isfamily = 0;
isfriend = 0;
ispublic = 1;
owner = "127012961#N08";
secret = 053032f300;
server = 2941;
title = "You #NYPL";
"url_m" = "https://farm3.staticflickr.com/2941/15166756998_053032f300.jpg";
"url_s" = "https://farm3.staticflickr.com/2941/15166756998_053032f300_m.jpg";
"url_sq" = "https://farm3.staticflickr.com/2941/15166756998_053032f300_s.jpg";
"url_t" = "https://farm3.staticflickr.com/2941/15166756998_053032f300_t.jpg";
"width_m" = 500;
"width_s" = 240;
"width_sq" = 75;
"width_t" = 100;
},
...etc.
I want to grab the 'photo' array. But I can't downcast 'anyObject' to 'Dictionary' so that I can subscript it. I tried:
var anyObject:Dictionary = responseDictionary["photos"]
But I got:
'(String, AnyObject)' is not convertible to '[String : AnyObject]'
So I'm stuck with:
var anyObject: AnyObject? = responseDictionary["photos"]
So with anyObject, I tried to access 'photos':
let RicPhotos = anyObject["Photo"] as [String : AnyObject]
...I also tried:
let RicPhotos = anyObject["Photo"] as Array<Dictionary<String,String>>
But I got:
'AnyObject?' does not have a member named 'subscript'
I can see the data, but I can't extract into an empty variable.
I attempted to downcast to a specific type (Dictionary) but the compiler refuses.
There must be a strait forward way of getting an embedded array from a dictionary whilst casting to its respective cast (without the 'anyObject').Any ideas?
Edited my answer to fit your edit...
let responseDictionary = [:] as [String : AnyObject]
var photosDic: AnyObject? = responseDictionary["photos"]
if let photosDic = photosDic as? Dictionary<String, AnyObject> {
var photosArray: AnyObject? = photosDic["photo"]
if let photosArray = photosArray as? Array<Dictionary<String, AnyObject>> {
//There you go
}
}