Skip to content

Commit

Permalink
more beta 6 changes: print syntax changed; variadic parameters don't …
Browse files Browse the repository at this point in the history
…have to be last; new suffix and prefix and pop and flatten; try?; more...
  • Loading branch information
mattneub committed Aug 26, 2015
1 parent 0a084f7 commit 99aabc3
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 9 deletions.
Expand Up @@ -18,6 +18,14 @@ func sayStrings(arrayOfStrings:String ...) {
for s in arrayOfStrings { print(s) }
}

// new in beta 6, variadic can go anywhere

func sayStrings(arrayOfStrings:String ..., times:Int) {
for _ in 1...times {
for s in arrayOfStrings { print(s) }
}
}

// ignored

func say(s:String, times:Int, loudly _:Bool) {Dog().say(s, times:times)}
Expand All @@ -39,6 +47,15 @@ class ViewController: UIViewController {
doThing(b:5, a:10) // legal but don't

sayStrings("hey", "ho", "nonny nonny no")

sayStrings("Mannie", "Moe", "Jack", times:3)

// print is now variadic

print("Mannie", 3, true) // Mannie 3 true

print("Mannie", "Moe", separator:", ", terminator: ", ")
print("Jack")

say("hi", times:3, loudly:true)

Expand Down
4 changes: 4 additions & 0 deletions bk1ch04p187generics/bk1ch04p187generics/ViewController.swift
Expand Up @@ -59,6 +59,8 @@ struct Insect3 : Flier3 {

// workaround:

///*

protocol Superflier3 {}

protocol Flier3 : Superflier3 {
Expand All @@ -72,6 +74,8 @@ struct Insect3 : Flier3 {
func flockTogetherWith(f:Insect3) {}
}

//*/


func flockTwoTogether2<T:Flier3>(f1:T, _ f2:T) {}
let vd2 : Void = flockTwoTogether2(Bird3(), Bird3())
Expand Down
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8152.3" systemVersion="14E46" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="8187.4" systemVersion="14F27" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8124.4"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8151.3"/>
</dependencies>
<scenes>
<!--View Controller-->
Expand Down
Expand Up @@ -4,7 +4,7 @@ import UIKit

extension Array {
mutating func shuffle () {
for i in stride(from:self.count-1, to:0, by:-1) {
for i in (0..<self.count).reverse() {
let ix1 = i
let ix2 = Int(arc4random_uniform(UInt32(i+1)))
(self[ix1], self[ix2]) = (self[ix2], self[ix1])
Expand Down
19 changes: 17 additions & 2 deletions bk1ch04p204arrays/bk1ch04p204collections/ViewController.swift
Expand Up @@ -124,6 +124,17 @@ class ViewController: UIViewController {
let arr4 = arr.suffix(10) // [1,2,3] with no penalty
print(arr4)

// new in beta 6

do {
let arr2 = arr.suffixFrom(1)
print(arr2)
let arr3 = arr.prefixUpTo(1)
print(arr3)
let arr4 = arr.prefixThrough(1)
print(arr4)
}

// let arr5 = arr[0..<10]

var r = arr.indices
Expand Down Expand Up @@ -199,9 +210,11 @@ class ViewController: UIViewController {
print(arr)
let i = arr.removeAtIndex(3)
let ii = arr.removeLast()
let iii = arr.popLast()
print(arr)
_ = i
_ = ii
_ = iii
let arr2 = arr.dropFirst()
let arr3 = arr.dropLast()
_ = arr2
Expand All @@ -213,9 +226,11 @@ class ViewController: UIViewController {
let arr2 = arr.joinWithSeparator([10,11]) // [1, 2, 10, 11, 3, 4, 10, 11, 5, 6]
let arr3 = arr.joinWithSeparator([]) // [1, 2, 3, 4, 5, 6]
let arr4 = arr.flatMap {$0} // new in Swift 1.2
let arr5 = Array(arr.flatten()) // new in Xcode 7 beta 6
_ = arr2
_ = arr3
_ = arr4
_ = arr5
}

do {
Expand Down Expand Up @@ -244,8 +259,8 @@ class ViewController: UIViewController {
let arr = [1,2,3]
let arr2 = arr.map {$0 * 2} // [2,4,6]
let arr3 = arr.map {Double($0)} // [1.0, 2.0, 3.0]
pepboys.map {print($0)} // prints Manny, then Moe, then Jack
pepboys.map(print)
pepboys.forEach {print($0)} // prints Manny, then Moe, then Jack
// pepboys.map(print) // no longer compiles
let arr4 = pepboys.filter{$0.hasPrefix("M")} // [Manny, Moe]

let arrr = [1, 4, 9, 13, 112]
Expand Down
Expand Up @@ -54,7 +54,7 @@ class ViewController: UIViewController {
print(s)
}

let keys = d.keys.array
let keys = Array(d.keys)

for (abbrev, state) in d {
print("\(abbrev) stands for \(state)")
Expand Down
9 changes: 8 additions & 1 deletion bk1ch05p239loops/bk1ch05p239loops/ViewController.swift
Expand Up @@ -11,6 +11,7 @@ enum Error {
class ViewController: UIViewController {

var movenda = [1,2,3]
var movenda2 = [1,2,3]

var boardView = UIView()

Expand All @@ -28,6 +29,12 @@ class ViewController: UIViewController {
}
}

do {
while let p = self.movenda2.popLast() {
print(p)
}
}

do {
let tvc = UITableViewCell()
let subview1 = UIView()
Expand Down Expand Up @@ -116,7 +123,7 @@ class ViewController: UIViewController {
}

do {
for i in stride(from: 10, through: 0, by: -2) {
for i in 10.stride(through: 0, by: -2) {
print(i) // 10, 8, 6, 4, 2, 0
}
}
Expand Down
5 changes: 5 additions & 0 deletions bk1ch05p240error/bk1ch05p241error.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
3269B3131B8D39B3009950F2 /* testing.txt in Resources */ = {isa = PBXBuildFile; fileRef = 3269B3121B8D39B3009950F2 /* testing.txt */; settings = {ASSET_TAGS = (); }; };
32F8A8B31B385DBA0042B53B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32F8A8B21B385DBA0042B53B /* AppDelegate.swift */; };
32F8A8B51B385DBA0042B53B /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32F8A8B41B385DBA0042B53B /* ViewController.swift */; };
32F8A8B81B385DBA0042B53B /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 32F8A8B61B385DBA0042B53B /* Main.storyboard */; };
Expand All @@ -15,6 +16,7 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
3269B3121B8D39B3009950F2 /* testing.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = testing.txt; sourceTree = "<group>"; };
32F8A8AF1B385DBA0042B53B /* bk1ch05p241error.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = bk1ch05p241error.app; sourceTree = BUILT_PRODUCTS_DIR; };
32F8A8B21B385DBA0042B53B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
32F8A8B41B385DBA0042B53B /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -56,6 +58,7 @@
children = (
32F8A8B21B385DBA0042B53B /* AppDelegate.swift */,
32F8A8B41B385DBA0042B53B /* ViewController.swift */,
3269B3121B8D39B3009950F2 /* testing.txt */,
32F8A8B61B385DBA0042B53B /* Main.storyboard */,
32F8A8B91B385DBA0042B53B /* Assets.xcassets */,
32F8A8BB1B385DBA0042B53B /* LaunchScreen.storyboard */,
Expand Down Expand Up @@ -124,6 +127,7 @@
32F8A8BD1B385DBA0042B53B /* LaunchScreen.storyboard in Resources */,
32F8A8BA1B385DBA0042B53B /* Assets.xcassets in Resources */,
32F8A8B81B385DBA0042B53B /* Main.storyboard in Resources */,
3269B3131B8D39B3009950F2 /* testing.txt in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -283,6 +287,7 @@
32F8A8C31B385DBA0042B53B /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
22 changes: 21 additions & 1 deletion bk1ch05p240error/bk1ch05p241error/ViewController.swift
Expand Up @@ -47,7 +47,27 @@ class ViewController: UIViewController {
print(error)
}

var err = SomeStruct()
// new better way in beta 6

do {
let f = "nonexistent" // path to some file, maybe
if let s = try? String(contentsOfFile: f, encoding: NSUTF8StringEncoding) {
print(s) // won't happen
} else {
print("I guess there was no such file, eh?")
}
}

lab: do {
// okay, I'm sick of failing, let's succeed for once :)
let f = NSBundle.mainBundle().pathForResource("testing", ofType: "txt")!
guard let s = try? String(contentsOfFile: f, encoding: NSUTF8StringEncoding)
else {print("still no file"); break lab}
print(s)
// if we get here, s is our string
}

let err = SomeStruct()
print(err._domain)
print(err._code)

Expand Down
1 change: 1 addition & 0 deletions bk1ch05p240error/bk1ch05p241error/testing.txt
@@ -0,0 +1 @@
This is a test
Expand Up @@ -69,6 +69,6 @@ class MyLayout : UICollectionViewLayout {

override func layoutAttributesForElementsInRect(rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
// println("rect")
return self.atts.values.array
return Array(self.atts.values)
}
}

0 comments on commit 99aabc3

Please sign in to comment.