I want to compare two objects(of the same Class) and return what is NOT nil.
How do you write something like this in swift? :
func returnWhatIsNotNil(objA: SomeClass, objB: SomeClass) -> SomeClass {
if objA != nil && objB != nil { //error
//both are not nil
return nil
}
if objA == nil && objB != nil { // error
// objA is nil
return objB
}
if objA != nil && objB == nil { // error
// objB is nil
return objA
}
}
with one object,
if let x = objA {
//objA is not nil
}
would do the job, but I quiet don't know how I would do this with TWO objects.
class SomeObject {
}
func returnWhatIsNotNil(objA: SomeObject?, objB: SomeObject?) -> SomeObject? {
if let objA = objA where objB == nil {
return objA
}
if let objB = objB where objA == nil {
return objB
}
return nil
}
edit: sorry, misread your original question. if you want nil when both are non-nil, then here is a one-liner that does it: return objA.map { objB == nil ? $0 : nil } ?? objB. However, #LeoDabus’s version using if…let…where looks more readable to me.
pre-edit assuming you want the first when neither are nil:
You want to use the nil-coalescing operator, ??. So to return an optional from your function, you just need return objA ?? objB. Note, all your input and output types also have to be optional.
Ordinarily you use it to give a default value in case of nil, i.e. "foo".toInt() ?? 0 which unwraps the value if non-nil, or replaces it with the default if its nil.
But if the right-hand side is also an optional, it will return an optional, with the left one if it’s non-nil, else the right one, even if that one is nil.
let i: Int? = nil
let j: Int? = nil
let k: Int? = 42
if let num = i ?? j {
fatalError("won't execute")
}
if let num = j ?? k {
assert(num == 42, "k will be chosen")
}
else {
fatalError("this would run if all 3 were nil")
}
Also bear in mind you can chain ?? too:
let x = i ?? j ?? k ?? 0 // will result in an unwrapped 42
As a follow up to #LeoDabus' accepted answer, you can use a switch statement also:
class SomeObject {
}
func returnWhatIsNotNil(objA: SomeObject?, objB: SomeObject?) -> SomeObject? {
switch (objA, objB) {
case (let a, nil): return a
case (nil, let b): return b
default: return nil
}
}
As mentioned the nil coalescing operator ?? can be used.
Here is an implementation of your function, note you didn't define what would happen when both objA and objB were non-nil
func returnWhatIsNotNil(objA: SomeClass?, objB: SomeClass?) -> SomeClass? {
return objA ?? objB
}
Although, a literal translation of your code in Swift is possible, which is
func returnNotNil(num1: Int?, num2: Int?) -> Int?
{
if let x = num1 {
return x
}
else if let y = num2
{
return y
}
else
{
return nil
}
}
The problem here is, in Swift, we will have to keep the return type as optional - so once returned from the function, you will again have to check for returned value being not nil, and unwrap.
IMHO, a logical translation would be, (assuming that you don't mind returning either, when none of them are nil)
func returnNotNilOrAssert(num1: Int?, num2: Int?) -> Int
{
if let x = num1 {
return x
}
else if let y = num2
{
return y
}
else
{
assert(false, "Both nil")
}
}
Whether or not to assert, is probably an implementation choice that you would have to make. Another way would be, if you're absolutely sure, that one of them would be not nil, that you make the return type of the function "implicitly unwrapped". I know you wanna return nil when both are nil, but then having to check the returned value of this function which is supposed to check two values and return the one which is not nil, IMHO wasn't making much sense, thus I tried it this way.
Related
func myFunc(array:[Int]) -> (min: Int, max: Int)?
{
if array.isEmpty {return nil}
var minNumber = array[0]
var maxNumber = array[0]
for number in array {
if number < minNumber {
minNumber = number
}
else if number > maxNumber{
maxNumber = number
}
}
return (minNumber, maxNumber)
}
let tempArray:[Int] = [1,2,3,4,5,6,7]
let value = myFunc(array: tempArray)
print("The minima is: \(value?.min != nil ? value!.min : nil) the maxima is \(value?.max != nil ? value!.max : nil)")
In the given code I just wanted to make if for example, the code contains some value it will force unwrap but if it is not contained it will just print "nil". But in my code, if it contains number it will print Optional(some number).
value?.min != nil ? value!.min : nil
is a (conditional) expression and evaluates to some value which has a type.
The first expression value!.min has the type Int, but the second expression nil is an optional and has the type Int?. Therefore the type of the conditional expression becomes Int? and that is printed as "Optional(1)".
What you want is the string "nil", or the non-nil value as a string:
print("The minimum is: \(value?.min != nil ? "\(value!.min)" : "nil")")
(and similarly for the maximum). Now both expression in the conditional expression
value?.min != nil ? "\(value!.min)" : "nil")
are strings, and the result is a string as well. This can be abbreviated to
print("The minimum is: \(value.map {"\($0.min)"} ?? "nil")")
If you need this frequently then you can define an extension method on the optional type
extension Optional {
var descriptionOrNil: String {
switch self {
case .some(let wrapped): return "\(wrapped)"
case .none: return "nil"
}
}
}
and use it as
print("The minimum is: \((value?.min).descriptionOrNil)")
So if i don't understand the question wrong. You can do:
extension Optional where Wrapped == Int {
var valueOrEmpty: String {
guard let unwrapped = self else {
return "nil"
}
return "\(unwrapped)"
}
}
print("The minima is: \(value?.min.valueOrEmpty) the maxima is \(value?.max.valueOrEmpty)")
Right?
without optional binding,we use optional like this,it seems tedious
func doSomething(str: String?)
{
let v: String! = str
if v != nil
{
// use v to do something
}
}
with optional binding,it seems the if let doesn't do any thing to make it less tedious. we still have a if statement to test before use it.
func doSomething(str: String?)
{
if let v = str
{
// use v to do something
}
}
Is there any other examples can show some benefits to use optional bindings ?
Advantages of the Optional binding over If Statements and Forced Unwrapping:
local variable which is not an optional and a shortcut when a structure is deeper than one level
Context:
You have three techniques to work with an optionals:
Optional binding
If statements and forced unwrapping
Optional chaining
Optional binding
You use optional binding to find out whether an optional contains a
value, and if so, to make that value available as a temporary constant
or variable. Optional binding can be used with if and while statements
to check for a value inside an optional, and to extract that value
into a constant or variable, as part of a single action.
If Statements and Forced Unwrapping
You can use an if statement to find out whether an optional contains a
value by comparing the optional against nil. You perform this
comparison with the “equal to” operator (==) or the “not equal to”
operator (!=).
Optional chaining
Optional chaining is a process for querying and calling properties,
methods, and subscripts on an optional that might currently be nil. If
the optional contains a value, the property, method, or subscript call
succeeds; if the optional is nil, the property, method, or subscript
call returns nil. Multiple queries can be chained together, and the
entire chain fails gracefully if any link in the chain is nil.
source
struct Computer {
let keyboard: Keyboard?
}
struct Keyboard {
let battery: Battery?
}
struct Battery {
let price: Int?
}
let appleComputer: Computer? = Computer(keyboard: Keyboard(battery: Battery(price: 10)))
func getBatteryPriceWithOptionalBinding() -> Int {
if let computer = appleComputer {
if let keyboard = computer.keyboard {
if let battery = keyboard.battery {
if let batteryPrice = battery.price {
print(batteryPrice)
return batteryPrice
}
}
}
}
return 0
}
func getBatteryPriceWithIfStatementsAndForcedUnwrapping() -> Int {
if appleComputer != nil {
if appleComputer!.keyboard != nil {
if appleComputer!.keyboard!.battery != nil {
if appleComputer!.keyboard!.battery!.price != nil {
print(appleComputer!.keyboard!.battery!.price!)
return appleComputer!.keyboard!.battery!.price!
}
}
}
}
return 0
}
func getBatteryPriceWithOptionalChainingAndForcedUnwrapping() -> Int {
if appleComputer?.keyboard?.battery?.price != nil {
print(appleComputer!.keyboard!.battery!.price!)
return appleComputer!.keyboard!.battery!.price!
}
return 0
}
func getBatteryPriceWithOptionalChainingAndOptionalBinding() -> Int {
if let price = appleComputer?.keyboard?.battery?.price {
print(price)
return price
}
return 0
}
func getBatteryPriceWithOptionalChainingAndNilCoalescing() -> Int {
print(appleComputer?.keyboard?.battery?.price ?? 0)
return appleComputer?.keyboard?.battery?.price ?? 0
}
I have a class which has property value of type Any I have implemented a didSet method for the value like this
didSet {
if oldValue == nil && self.value != nil {
// do something
}
else {
if let val = self.value as? AnyHashable, let oldVal = oldValue as? AnyHashable {
if val != oldVal {
// do something
}
}
}
}
My value is optional type so I want to verify the nil case too.
Is there are better way of doing that I do not want to cast to Anyhashasble I want them to be casted to the class they belong.
Well, if you assume that this property is aways, and if only always, a reference type, then you can try to use AnyObject instead of Any and use === operator to check if pointers are pointing to same object.
Something like this:
class Test {
var object: AnyObject? {
didSet {
if oldValue === object {
debugPrint("Old object")
} else {
debugPrint("New object")
}
}
}
}
class SomeClass {}
let test = Test()
let s = SomeClass()
test.object = s
test.object = s
test.object = SomeClass()
The result will be:
"New object" - because previous value was nil
"Old object" - because we assigned same s
"New object" - because we assigned completely new object
I have this code in Swift:
guard let user = username else{
return nil
}
But I'm getting the following errors:
Nil is incompatible with return type String
Any of you knows why or how I return nil in this case?
I'll really appreciate your help
Does your function declare an optional return type?
func foo() -> String? { ...
See more on: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html
NOTE
The concept of optionals doesn’t exist in C or Objective-C. The
nearest thing in Objective-C is the ability to return nil from a
method that would otherwise return an object, with nil meaning “the
absence of a valid object.”
You have to tell the compiler that you want to return nil. How do you that? By assigning ? after your object. For instance, take a look at this code:
func newFriend(friendDictionary: [String : String]) -> Friend? {
guard let name = friendDictionary["name"], let age = friendDictionary["age"] else {
return nil
}
let address = friendDictionary["address"]
return Friend(name: name, age: age, address: address)
}
Notice how I needed to tell the compiler that my object Friend, which I'm returning, is an optional Friend?. Otherwise it will throw an error.
*Does your function declare an optional return type?
func minAndmax(array:[Int])->(min:Int, max:Int)? {
if array.isEmpty {
return nil
}
var currentMin = array[0]
var currentMax = array[0]
for value in array {
if value < currentMin {
currentMin = value
}
else if value > currentMax {
currentMax = value
}
}
return (currentMin, currentMax)
}
if let bounds = minAndmax(array: [8, -6, 2, 109, 3, 71]) {
print(bounds)
}
In Objective-C, when I have an array
NSArray *array;
and I want to check if it is not empty, I always do:
if (array.count > 0) {
NSLog(#"There are objects!");
} else {
NSLog(#"There are no objects...");
}
That way, there is no need to check if array == nil since this situation will lead the code to fall into the else case, as well as a non-nil but empty array would do.
However, in Swift, I have stumbled across the situation in which I have an optional array:
var array: [Int]?
and I am not being able to figure out which condition to use. I have some options, like:
Option A: Check both non-nil and empty cases in the same condition:
if array != nil && array!.count > 0 {
println("There are objects")
} else {
println("No objects")
}
Option B: Unbind the array using let:
if let unbindArray = array {
if (unbindArray.count > 0) {
println("There are objects!")
} else {
println("There are no objects...")
}
} else {
println("There are no objects...")
}
Option C: Using the coalescing operator that Swift provides:
if (array?.count ?? 0) > 0 {
println("There are objects")
} else {
println("No objects")
}
I do not like the option B very much, because I am repeating code in two conditions. But I am not really sure about whether options A and C are correct or I should use any other way of doing this.
I know that the use of an optional array could be avoided depending on the situation, but in some case it could be necessary to ask if it is empty. So I would like to know what is the way to do it the simplest way.
EDIT:
As #vacawama pointed out, this simple way of checking it works:
if array?.count > 0 {
println("There are objects")
} else {
println("No objects")
}
However, I was trying the case in which I want to do something special only when it is nil or empty, and then continue regardless whether the array has elements or not. So I tried:
if array?.count == 0 {
println("There are no objects")
}
// Do something regardless whether the array has elements or not.
And also
if array?.isEmpty == true {
println("There are no objects")
}
// Do something regardless whether the array has elements or not.
But, when array is nil, it does not fall into the if body. And this is because, in that case, array?.count == nil and array?.isEmpty == nil, so the expressions array?.count == 0 and array?.isEmpty == true both evaluate to false.
So I am trying to figure out if there is any way of achieve this with just one condition as well.
Updated answer for Swift 3 and above:
Swift 3 has removed the ability to compare optionals with > and <, so some parts of the previous answer are no longer valid.
It is still possible to compare optionals with ==, so the most straightforward way to check if an optional array contains values is:
if array?.isEmpty == false {
print("There are objects!")
}
Other ways it can be done:
if array?.count ?? 0 > 0 {
print("There are objects!")
}
if !(array?.isEmpty ?? true) {
print("There are objects!")
}
if array != nil && !array!.isEmpty {
print("There are objects!")
}
if array != nil && array!.count > 0 {
print("There are objects!")
}
if !(array ?? []).isEmpty {
print("There are objects!")
}
if (array ?? []).count > 0 {
print("There are objects!")
}
if let array = array, array.count > 0 {
print("There are objects!")
}
if let array = array, !array.isEmpty {
print("There are objects!")
}
If you want to do something when the array is nil or is empty, you have at least 6 choices:
Option A:
if !(array?.isEmpty == false) {
print("There are no objects")
}
Option B:
if array == nil || array!.count == 0 {
print("There are no objects")
}
Option C:
if array == nil || array!.isEmpty {
print("There are no objects")
}
Option D:
if (array ?? []).isEmpty {
print("There are no objects")
}
Option E:
if array?.isEmpty ?? true {
print("There are no objects")
}
Option F:
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
Option C exactly captures how you described it in English: "I want to do something special only when it is nil or empty." I would recommend that you use this since it is easy to understand. There is nothing wrong with this, especially since it will "short circuit" and skip the check for empty if the variable is nil.
Previous answer for Swift 2.x:
You can simply do:
if array?.count > 0 {
print("There are objects")
} else {
print("No objects")
}
As #Martin points out in the comments, it uses func ><T : _Comparable>(lhs: T?, rhs: T?) -> Bool which means that the compiler wraps 0 as an Int? so that the comparison can be made with the left hand side which is an Int? because of the optional chaining call.
In a similar way, you could do:
if array?.isEmpty == false {
print("There are objects")
} else {
print("No objects")
}
Note: You have to explicitly compare with false here for this to work.
If you want to do something when the array is nil or is empty, you have at least 7 choices:
Option A:
if !(array?.count > 0) {
print("There are no objects")
}
Option B:
if !(array?.isEmpty == false) {
print("There are no objects")
}
Option C:
if array == nil || array!.count == 0 {
print("There are no objects")
}
Option D:
if array == nil || array!.isEmpty {
print("There are no objects")
}
Option E:
if (array ?? []).isEmpty {
print("There are no objects")
}
Option F:
if array?.isEmpty ?? true {
print("There are no objects")
}
Option G:
if (array?.count ?? 0) == 0 {
print("There are no objects")
}
Option D exactly captures how you described it in English: "I want to do something special only when it is nil or empty." I would recommend that you use this since it is easy to understand. There is nothing wrong with this, especially since it will "short circuit" and skip the check for empty if the variable is nil.
Extension Property on the Collection Protocol
*Written in Swift 3
extension Optional where Wrapped: Collection {
var isNilOrEmpty: Bool {
switch self {
case .some(let collection):
return collection.isEmpty
case .none:
return true
}
}
}
Example Use:
if array.isNilOrEmpty {
print("The array is nil or empty")
}
Other Options
Other than the extension above, I find the following option most clear without force unwrapping optionals. I read this as unwrapping the optional array and if nil, substituting an empty array of the same type. Then, taking the (non-optional) result of that and if it isEmpty execute the conditional code.
Recommended
if (array ?? []).isEmpty {
print("The array is nil or empty")
}
Though the following reads clearly, I suggest a habit of avoiding force unwrapping optionals whenever possible. Though you are guaranteed that array will never be nil when array!.isEmpty is executed in this specific case, it would be easy to edit it later and inadvertently introduce a crash. When you become comfortable force unwrapping optionals, you increase the chance that someone will make a change in the future that compiles but crashes at runtime.
Not Recommended!
if array == nil || array!.isEmpty {
print("The array is nil or empty")
}
I find options that include array? (optional chaining) confusing such as:
Confusing?
if !(array?.isEmpty == false) {
print("The array is nil or empty")
}
if array?.isEmpty ?? true {
print("There are no objects")
}
Swift extension:
extension Optional where Wrapped: Collection {
var nilIfEmpty: Optional {
switch self {
case .some(let collection):
return collection.isEmpty ? nil : collection
default:
return nil
}
}
var isNilOrEmpty: Bool {
switch self {
case .some(let collection):
return collection.isEmpty
case .none:
return true
}
}
Usage:
guard let array = myObject?.array.nilIfEmpty else { return }
or:
if myObject.array.isNilOrEmpty {
// Do stuff here
}
Option D: If the array doesn't need to be optional, because you only really care if it's empty or not, initialise it as an empty array instead of an optional:
var array = [Int]()
Now it will always exist, and you can simply check for isEmpty.
Conditional unwrapping:
if let anArray = array {
if !anArray.isEmpty {
//do something
}
}
EDIT: Possible since Swift 1.2:
if let myArray = array where !myArray.isEmpty {
// do something with non empty 'myArray'
}
EDIT: Possible since Swift 2.0:
guard let myArray = array where !myArray.isEmpty else {
return
}
// do something with non empty 'myArray'
The elegant built-in solution is Optional's map method. This method is often forgotten, but it does exactly what you need here; it allows you to send a message to the thing wrapped inside an Optional, safely. We end up in this case with a kind of threeway switch: we can say isEmpty to the Optional array, and get true, false, or nil (in case the array is itself nil).
var array : [Int]?
array.map {$0.isEmpty} // nil (because `array` is nil)
array = []
array.map {$0.isEmpty} // true (wrapped in an Optional)
array?.append(1)
array.map {$0.isEmpty} // false (wrapped in an Optional)
It's better to use if to check for empty array. Why bcoz, for example if we try to find greatest integer in a list array, obviously we will compare list[0] with every integer in a list. at that time it gives us Index out of range exception.... you can try this with both IF & Guard
func findGreatestInList(list: [Int]?)-> Int? {
if list!.count == 0 {
print("List is empty")
return nil
}
/*guard list!.isEmpty else {
print("List is empty")
return nil
}*/
var greatestValue = list![0]
for number in 0...list!.count-1 {
if list![number] > greatestValue {
greatestValue = list![number]
Instead of using if and else it is better way just to use guard to check for empty array without creating new variables for the same array.
guard !array.isEmpty else {
return
}
// do something with non empty ‘array’