it's been 1.5 years i've been writing in obj-c
, its a good language.. so i saw a medium article today about swift, really looking onto it, right now i am trying to convert all my obj-c
codes to swift
, fortunately everything is done expect for this part..
NSString *path = [[NSBundle mainBundle] pathForResource:@"list" ofType:@"plist"];
NSArray *plistData = [NSArray arrayWithContentsOfFile:path];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"english ==[c] %@", self.userQuery.text]; // something like "Abbey"
NSArray *filtered = [plistData filteredArrayUsingPredicate:filter];
NSLog(@"found matches: %@ : %@", filtered,[filtered valueForKey:@"kurdi"]);
if (filtered.count>0) {
NSDictionary *dic = filtered[0];
self.theMeaningLabel.text = dic[@"kurdi"];
} else {
self.theMeaningLabel.text = @"Yay!";
}
i am not being able to properly convert this into the new swift 4
, it gives random errors
EDIT
after a few searches, i could just write two lines of code here's my swift code
var path: String? = Bundle.main.path(forResource: "list", ofType: "plist")
var plistData = [Any](contentsOfFile: path!)
var filter = NSPredicate(format: "english ==[c] %@", searchText)
// something like "Abbey"
var filtered = plistData?.filter { filter.evaluate(with: $0) }
print("found matches: \(filtered) : \(filtered?.value(forKey: "kurdi"))")
if filtered?.count > 0 {
var dic = filtered[0]
// theMeaningLabel.text = dic["kurdi"]
}
else {
//theMeaningLabel.text = "Yay!"
}
but getting the
Argument labels '(contentsOfFile:)' do not match any available overloads
Final edit
var path = Bundle.main.path(forResource:"list", ofType: "plist")
var plistData = NSArray(contentsOfFile: path!)
let filter = NSPredicate(format: "english ==[c] %@", searchText)
var filtered = plistData?.filtered(using: filter)
// [error 9:10] no viable alternative at input 'NSLog(@"found matches: %@ : %@"'
if filtered?.count > 0 {
var dic = filtered![0]
// theMeaningLabel.text = dic["kurdi"]
}
else {
// theMeaningLabel.text = "Yay!"
}
the code above is fine, but one error comes
if filtered?.count > 0 { // here
var dic = filtered![0]
// theMeaningLabel.text = dic["kurdi"]
}
else {
// theMeaningLabel.text = "Yay!"
}
getting
Binary operator '>' cannot be applied to operands of type 'Int?' and 'Int'