boolean return help to find fix - ios

i have simple problem I want to return this one:
let results = prices.filter{
if($0.MultipleSearchString != nil){
predicate.evaluate(with: ($0.MultipleSearchString?.lowercased())!)
}else {
self.loadPricesFromDb()
}
}
but I have problem with it, because it giving problem like:
Missing return in a closure expected to return 'Bool' and I dunno why
Can someone explain me this?

You're using the filter function wrong.
Filter with an inline closure will evaluate each member of the array with the logic in the provided closure, and return an array of elements that satisfy that logic, e.g.:
let results = prices.filter {
$0.MultipleSearchString != nil
}
The above will have results with all elements that have MultipleSearchString different from nil.
It's also bad to call a function that presumably loads the data from database since it will trigger on (almost) every iteration.
You need to refactor your code to better work with your intended solution.

Related

Printing value of $0

I have a certain line of code which looks like so...
let myMessages = theObjects.filter { $0.myJid.user == user.JID.user }
Now I want to print the value of $0.myJid.user. How can I achieve that..?
Another option:
let myMessages = theObjects.filter { print($0.myJid.user); return $0.myJid.user == user.JID.user }
You can do it like this.
let myMessages = theObjects.filter { (value) -> Bool in
print(value.myJid)
return value.myJid.user == user.JID.user
}
Tip: Naming your variable user seems like a bad idea since you already have a parameter with the same name for JID.
Edit: Seems i was wrong about not being able to print $0. You can do that just like in the regular closure. The only difference being you can omit the argument list and you need to add a return. To print using shorthand argument, check #Sateesh's answer.

How to return nothing from a method with return type of id

I have the following method:
- (id) insertDictationResultPlaceholder
{
return nil;
}
The Xcode analyzer is barking at me and telling me:
nil returned from a method that is expected to return a non-null value
So, for a method with a return parameter of id, how do I return nothing? Would an empty string be good? What's the nicest solution here?
You can use [NSNull null] instead. Just be cautious when you're checking for it, because it does NOT equate to nil if you try to do a comparison:
if ([NSNull null] == nil)
{
// This code will never run
}
I want to understand why do you want to return nil as a return, Instead you can use the blocks for call back mechanism.
If you want to use function itself, it would be better if you can send empty object(may be array or dictionary or string) based on the return type expected in the function.

How to avoid If let in Swift before doing a for loop

I have this code on Swift:
let items = doSomethingFuncToGetDataWithOptionalResults()
if let items = items {
for item in items {
// so something...
}
}
Could anyone can help me to avoid if let ... in this case. It would be better if we could ignore if let in this case. I feel annoyed when writing this statements every time.
Regards,
Generally, if a function returns an optional then you can use
optional chaining to operate on the result only if it is not nil.
In your case of an optional array you can use
optional chaining and forEach():
doSomethingFuncToGetDataWithOptionalResults()?.forEach { item in
// do something with `item` ...
}
The forEach() statement will not be executed if the function
returns nil.
You can do something like this:
if let items = doSomethingFuncToGetDataWithOptionalResults() {
for item in items {
// so something...
}
}

How to prettify enum with arguments comparison in Swift?

This is what I have
let kind = //This returns one of the cases with it corresponding arguments
if kind == .didChangeValue(value: nil) {
//my Stuff
}
this is what I want:
if kind == .didChangeValue {
//my Stuff
}
Notice that:
This is happening because my enum has arguments, I already implemented how they should compare with each other and the value has no value to me.
So, I'm trying to get it to look more swifty and less like a RAW HACK
You can check an enumeration value with pattern matching:
if case .didChangeValue = kind {
// ...
}

Type cast array elements in for loop

In a delegate method, I get back a ‘results’ array of a custom object type, and I want to loop through the array elements.
I do the following now, and this works
for result in results {
if result is XYZClass {
//This Works!
}
}
Is there a way to type cast the objects in the for-loop to avoid writing two lines? Does swift permit this? Used to get this done fairly easily in Objective - C
for (XYZClass *result in results) {
}
However, I have not been successful in Swift. I’ve tried explicit-cast with no luck.
for result as XYZClass in results {
//ERROR: Expected ‘;’ in ‘for’ statements
}
for result:AGSGPParameterValue in results {
/* ERROR: This prompts down cast as
for result:AGSGPParameterValue in results as AGSGPParameterValue { }
which in turn errors again “Type XYZClass does not conform to Sequence Type”
*/
}
Any help is appreciated
Try this:
for result in results as [XYZClass] {
// Do stuff to result
}
Depending on how you are using the for loop it may be instead preferable to use compactMap (or flatMap if you are before Swift 4.1) to map your objects into a new array:
let onlyXyzResults: [XYZClass] = results.compactMap { $0 as? XYZClass }
Now you have an array XYZClass objects only, with all other object types removed.

Resources