swift-playground

How is it possible to call a function with parameters without passing in values? [Swift Playground]

有些话、适合烂在心里 提交于 2019-12-12 05:28:21
问题 Swift Playground provided the following code. How's the speakText(graphic: )called without passing in parameters? (Obviously graphic is already placed in another segment of the code) // Speak the text of graphic. func speakText(graphic: Graphic) { speak(graphic.text) } func addGreeting(touch: Touch) { if touch.previousPlaceDistance < 60 { return } let greetings = ["howdy!", "hello", "hi", "ciao", "yo!", "hey!", "what’s up?"] let greeting = greetings.randomItem let graphic = Graphic(text:

Why does UInt32(stringArray.count) count wrong in a function but correct alone in swift playground?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-12 04:58:21
问题 Why does this code count 6 elements, as 9 ("wrong") in swift playground. var stringArray = ["1", "2", "3", "4", "5", "6"] for var i = 0; i < 3; i++ { stringArray.append("Paragraph" + "\(i)") } func concat (array: [String]) -> String { let count = UInt32(stringArray.count) ** --> =9 ** let randomNumberOne = Int(arc4random_uniform(count)) let randomNumberTwo = Int(arc4random_uniform(count)) let randomNumberThree = Int(arc4random_uniform(count)) let concatString = array[randomNumberOne] + array

Swift Addition: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (Code=EXC_l386_INVOP, subcode=0x0)

我只是一个虾纸丫 提交于 2019-12-12 03:34:09
问题 I'm trying to add two numbers in Swift and print its sum import Foundation func solveMefirst(firstNo: Int , secondNo: Int) -> Int { return firstNo + secondNo } let num1 = readLine() let num2 = readLine() var IntNum1 = Int(num1!) ** Execution was interrupted, reason: EXC_BAD_INSTRUCTION (Code=EXC_l386_INVOP, subcode=0x0). ** var IntNum2 = Int(num2!) let sum = solveMefirst(IntNum1!, secondNo: IntNum2!) print(sum) But unfortunately this error comes out and stops the execution in playground.

Differences between Playground and Project

元气小坏坏 提交于 2019-12-11 19:34:30
问题 In the course of answering another question, I came across a weird bug in Playground. I have the following code to test if an object is an Array , Dictionary or a Set : import Foundation func isCollectionType(value : AnyObject) -> Bool { let object = value as! NSObject return object.isKindOfClass(NSArray) || object.isKindOfClass(NSDictionary) || object.isKindOfClass(NSSet) } var arrayOfInt = [1, 2, 3] var dictionary = ["name": "john", "age": "30"] var anInt = 42 var aString = "Hello world"

How Can I Add DeviceMotion Capabilities to a Swift Playground?

自作多情 提交于 2019-12-11 15:23:19
问题 I am working on a Swift playground and I am trying to use this code to get the device motion. @objc func update() { if let deviceMotion = motionManager.deviceMotion { print("Device Motion Yaw: \(deviceMotion.attitude.yaw)") } } However, it seems that device motion does not work on a Swift playground even though it works in iOS. How would I change a playground to support device motion? I am using an iPad running iOS 12 and the latest version of Swift Playgrounds and a Mac for the code. I know

Runtime for selected device error in playground

若如初见. 提交于 2019-12-11 12:59:00
问题 I am working in the Playground. I accidentally was using OSX playgrounds instead of iOS Playgrounds, so I opened a new Playground for iOS. When I opened the Playground I immediately got this error: "The runtime for the selected device is not installed." This error pops up every time I try to type anything into the playground. This did not happen in the Playgrounds I created that were OSX. Yes, I turned it off and back on again. :) I see in this question: The runtime for selected device is not

How to import my own class into an Xcode 7 playground?

纵饮孤独 提交于 2019-12-11 12:15:32
问题 I know this question was asked before, but I didn't find an answer in relation to Xcode 7 and Swift 2. Many answers are just ambiguous, incorrect or outdated. Is there a clear way to import a single class or a set of classes to a playground? 回答1: In the Playground go to menu View / Navigators / Show Project Navigator and copy your class files into the Sources folder. Your classes have to be marked public like this: public class MyPlayGround { public class func sayHello() { print("Hello") } }

Can't get my Timer in Swift to fire in Playground

匆匆过客 提交于 2019-12-11 11:58:17
问题 I'm trying to make a nice timer using Swift and GCD. I find lots of blogs and even an entry in Matt Neuburg's book. I used the latter to put together my own variant in a playground: import Foundation struct Timer { private var queue = dispatch_queue_create("timer", nil) private var source: dispatch_source_t var tick:()->() = {} { didSet { self.update() } } var rate:Double = 1.0 { didSet { self.update() } } init() { self.source = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, self

Could not find member <method name> for struct type in Swift

落花浮王杯 提交于 2019-12-11 11:43:46
问题 I have the following code but when I try to chain using the following x.initial(2).flatten() I get a compile error saying Could not find member flatten What am I doing wrong. To make sure I can keep chaining I am returning self after each chained method such as flatten, initial, etc. struct $ { var resultArray: AnyObject[] = [] init(array: AnyObject[]) { self.resultArray = array } mutating func first() -> AnyObject? { return $.first(self.resultArray) } mutating func flatten() -> $ { self

Xcode Playgrounds Shared Directory Not Working

自古美人都是妖i 提交于 2019-12-11 07:38:39
问题 When creating an Xcode Playground, the supported playgroundSharedDataDirectory does not properly show my the expected directory. Expected: ~/Documents/Shared Playground Data Actual: file:///var/folders/46/zg_mg07d5h5_9t6q_4vr9_2w0000gn/T/com.apple.dt.Xcode.pg/containers/com.apple.dt.playground.stub.iOS_Simulator.MyPlayground-BEF6F13E-994E-45C9-A18E-7DE953069D69/Documents/Shared%20Playground%20Data/ import UIKit import PlaygroundSupport print(playgroundSharedDataDirectory) This also does not