error: Value of optional type 'String?' not unwrapped; did you mean to use '!' or '?'?import Founda

22 views
Skip to first unread message

G G

unread,
Jun 14, 2015, 7:51:23 PM6/14/15
to swift-l...@googlegroups.com

import Foundation

// Fig03-01-11: Account.swift

// Account class with name and balance properties,

// an initializer adn deposit and withdraw methods


public class Account {

public var name: String = ""

public private(set) var balance: Double = 0.0

public init(name: String, balance: Double) {

self.name = name

if balance > 0.0 {

self.balance = balance

}

}

public func deposit(amount: Double) {

if amount > 0.0 {

balance = balance + amount

}

}

public func withdraw(amount: Double) {

if amount > 0.0 {

if balance - amount >= 0.0 {

balance = balance - amount

}

}

}

} // end class Account


/***********************************************************/



import Foundation



// fig03-01-11: main.swift

// Using class Account's init method to initialize and Account's

// name property when the Account object is created



var formatter = NSNumberFormatter()

formatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle


func formatAccountString(account: Account) -> String {

return account.name + "'s balance: " +

formatter.stringFromNumber(account.balance)!

}


let account1 = Account(name: "Jane Green", balance: 50.00)

let account2 = Account(name: "John Blue", balance: -7.53)


// display initial balance of each Account

println(formatAccountString(account1))

println(formatAccountString(account2))


var depositAmount = 25.53


println("\ndepositing " + formatter.stringFromNumber(depositAmount) +

" into accounting1\n") // error here

account1.deposit(depositAmount)


depositAmount = 123.45

println("\ndepositing " + formatter.stringFromNumber(depositAmount) +

" into account2\n")  // error here

account2.deposit(depositAmount)


println(formatAccountString(account1))

println(formatAccountString(account2))


var withdrawalAmount = 14.27


println("\nwithdrawing " + formatter.stringFromNumber(withdrawalAmount) +

" from account1\n")   // error here

account1.withdraw(withdrawalAmount)


println(formatAccountString(account1))

println(formatAccountString(account2))


withdrawalAmount = 100.00

println("\nwithdrawing " + formatter.stringFromNumber(withdrawalAmount) +

" from account2\n")   // error here

account2.withdraw(withdrawalAmount)


println(formatAccountString(account1))

println(formatAccountString(account2))



/* how do i fix this and why does it not just work */

/* code from deitel and deitel swift for programmers */


G G

unread,
Jun 14, 2015, 7:54:30 PM6/14/15
to swift-l...@googlegroups.com


On Sunday, June 14, 2015 at 7:51:23 PM UTC-4, G G wrote
/* how do i fix this */

ok i fixed it by adding the explanation mark. 
why was it necessary?

Brent Royal-Gordon

unread,
Jun 15, 2015, 6:52:01 PM6/15/15
to G G, swift-l...@googlegroups.com
NSNumberFormatter.stringFromNumber() is declared to return an optional string (written "String?" in the headers), so that the method can return nil if there's a problem formatting the number. (I believe the main error that can occur is that the value you're formatting might be lower than the minimum or higher than the maximum, but I could be wrong.)

Swift requires you to handle this error case in some way. Adding an "!" tells Swift that you don't think it should be possible for this call to fail, and that if it does, your app should crash. Other options include substituting a default value with the "??" operator or moving the number formatting call into an "if let" or (in Swift 2) "guard let" statement to handle the nil case.

--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/fdfdc0a3-1326-489e-aac8-b6556dca15bb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

G G

unread,
Jun 16, 2015, 10:42:29 PM6/16/15
to swift-l...@googlegroups.com, gdo...@gmail.com

thanks, Brent
Reply all
Reply to author
Forward
0 new messages