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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
G G
Reply | Threaded
Open this post in threaded view
|

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

G G

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 */


--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/2273418b-b838-4eb3-8feb-4728b4787cc0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
G G
Reply | Threaded
Open this post in threaded view
|

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

G G


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?

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

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

Brent Royal-Gordon-2
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.

On Sun, Jun 14, 2015 at 4:54 PM G G <[hidden email]> wrote:


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?

--
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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CAEeRk-agYmxcmFY5ftxUCim80O1NDGzqQkhG2m_XVbfChHHG9w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
G G
Reply | Threaded
Open this post in threaded view
|

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

G G

thanks, Brent

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/c850e6bd-5ed8-459a-9b11-0fdde4c612ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.