Refactor
Refactor

Reputation: 524

swift tuple has unexpected print result

The following code in playground works as expected.

let abc = 5.0
let def = 10.0
let tuple = (a: abc, d: def)
println("   tuple Parts  .0 = \(tuple.0)  .1 = \(tuple.1) ")
println("   tuple Parts  .a = \(tuple.a)  .d = \(tuple.d) ")
println("   tuple Whole = \(tuple)")

Playground console output:

tuple Parts  .0 = 5.0  .1 = 10.0 
tuple Parts  .a = 5.0  .d = 10.0 
tuple Whole = (5.0, 10.0)

When that code is placed in a function in a swift class, and a build/run done, the console shows:

tuple Parts  .0 = 5.0  .1 = 10.0
tuple Parts  .a = 5.0  .d = 10.0 
tuple Whole = (1.28601959704862e-313, 1.28601959783912e-313)

The Whole is showing numbers that are almost zero. Running Beta 2.

What am I overlooking?
Also curious is that placing the println lines in a loop prints out slightly different e-313 values for each repitition of the Whole line.

MORE For those requesting the rest of the code, there isn't much.

A minimalist, quick and dirty, "full code example" is to create a new project, an iOS Application, choosing Single View Application. Swift Language selected.

Add a tryTuple function to the View Controller and a call to that function in viewDidLoad.

ViewController.swift looks like:

import UIKit

class ViewController: UIViewController {

func tryTuple (marker:String) {

    println("---- \(marker)")
    let abc = 5.0
    let def = 10.0
    let tuple = (a: abc, d: def)
    println("   tuple Parts  .0 = \(tuple.0)  .1 = \(tuple.1) ")
    println("   tuple Parts  .a = \(tuple.a)  .d = \(tuple.d) ")
    println("   tuple Whole = \(tuple)")
}

override func viewDidLoad() {
    super.viewDidLoad()
    tryTuple("TryTuple1")
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

Run of this (pressing the Build and Run cursor in XCODE, happen to be using iPhone 4s iOS simulator) produces this console output.

---- TryTuple1
   tuple Parts  .0 = 5.0  .1 = 10.0 
   tuple Parts  .a = 5.0  .d = 10.0 
   tuple Whole = (1.28543029649464e-313, 1.28543029728515e-313)

So still isn't what I expect.

Upvotes: 1

Views: 1234

Answers (1)

Matt Gibson
Matt Gibson

Reputation: 38238

Looks like a bug to me. You don't need to put this into a separate function; just putting this:

    let foo = (5.0, 10.0)
    println("foo: \(foo)")

...into viewDidLoad in an iOS project will reproduce the problem when run on a 32-bit simulator, as far as I can tell. This may explain why you can't reproduce it in a playground: I'm guessing your playground will be 64-bit. If I run the above code on a 4S simulator, it prints zeroes, if I run it in a 5S simulator, it prints the expected values.

I'd file a bug if I were you.

Upvotes: 1

Related Questions