• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Swift question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Swift question


  • Subject: Re: Swift question
  • From: Frédéric Testuz <email@hidden>
  • Date: Wed, 04 Jun 2014 20:23:15 +0200

The compiler should not complain if it’s just this.

I think in this case, inside the if block you are declaring a new const variable. The hammerTitle inside the if block is not the same as hammerTitle outside. When the code exit the if block the inside hammerTitle is out of scope.

Frédéric


Le 4 juin 2014 à 20:17, Carl Leiby <email@hidden> a écrit :

1. HammerTitle can't be changed because you used let. 
2. You didn't change it in the if you defined a new constant in that scope. 

Hope that helps
Carl




On Wed, Jun 4, 2014 at 11:13 AM -0700, "David Delmonte" <email@hidden> wrote:

As there does not seem to be any appropriate place for this question, I’m guessing to post this here:

I’m starting through the enjoyable Guided Tour of Swift :(https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/GuidedTour.html#//apple_ref/doc/uid/TP40014097-CH2-XID_1)

and have a simple question…

        let nails = 100
        var hammers = 1
        
        let hammerTitle = " hammers"
        if hammers == 1  {
            let hammerTitle = " hammer"
        }
        let toolSummary = "I have \(nails) nails and \(hammers)" + hammerTitle

This always results in:

toolSummary: I have 100 nails and 1 hammers

Any ideas what I’m doing wrong?
(for non-english speakers, I want “1 hammer”)

Thanks


Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Re: Swift question (From: Carl Leiby <email@hidden>)

  • Prev by Date: Re: Swift question
  • Next by Date: Re: Swift question
  • Previous by thread: Re: Swift question
  • Next by thread: Re: Swift question
  • Index(es):
    • Date
    • Thread