swift-playground

How does NSData(bytes:length:) convert [Byte] to UnsafePointer<Void> in Swift?

泪湿孤枕 提交于 2019-12-11 07:26:50
问题 Playing with raw data in Swift I came across something I don't understand. NSData has a constructor: init(bytes: UnsafePointer<Void>, length: Int) where the first bytes parameter is clearly of UnsafePointer type. However, if I pass [Byte] object to this constructor, not only does the compiler not complain, but it works ok. But if I try to cast [Byte] to UnsafePointer , I fail. How does this work? For example (you can try it in Playgrounds): let buffer: [Byte] = [0x00, 0xff] let data = NSData

Detecting end of the playback in AVAudioPlayer

天大地大妈咪最大 提交于 2019-12-11 05:32:21
问题 I've got couple of short .mp3 sounds which I'm storing in the array and would like to play them consecutively. Is there any way to detect when AVAudioPlayer stopped playing so that I could call a completion handler and play next sound? I know that there is a delegate for that but I'm using Playground and SKScene. 回答1: Hellp @codddeer123, AVAudioPlayer contains AVAudioPlayerDelegte method which is called on audio playing completed. I have implemented it as extension of my class, you can do as

EXC_I386_GPFLT in Swift code using Xcode Playground

一世执手 提交于 2019-12-11 04:19:28
问题 I ran the same code in Xcode 7beta/rc Playground project and got an error: Execution was interrupted, reason: EXC_BAD_ACCESS(code=EXC_I386_GPFLT) in let n: Int = Int(Process.arguments[1])! How do I solve in Playground project since other solutions don't seem to be related? Binary tree: http://benchmarksgame.alioth.debian.org/u64q/program.php?test=binarytrees&lang=swift&id=1 class TreeNode { var left, right : TreeNode? var item : Int init(_ left: TreeNode?, _ right: TreeNode?, _ item: Int) {

use of undeclared type 'Date' Xcode 9 Swift 4

ぐ巨炮叔叔 提交于 2019-12-11 02:29:24
问题 This is my code in xcode 9 playground class Tutorial: Codable { let title: String let author: String let editor: String let type: String let publishDate: Date init(title: String, author: String, editor: String, type: String, publishDate: Date) { self.title = title self.author = author self.editor = editor self.type = type self.publishDate = publishDate } } its showing error use of undeclared type 'Date' 回答1: The Date struct is part of the Foundation framework. Add import Foundation at the

Linking pages in Swift Playgrounds [Xcode]

孤者浪人 提交于 2019-12-11 01:53:58
问题 I am new to swift playgrounds and ran into some problems while making a swift playground in Xcode. This is my main Playground page import UIKit import PlaygroundSupport import SpriteKit let secondScene = Index() let master = FirstScene() let root = UINavigationController(rootViewController: master) PlaygroundPage.current.liveView = root But when I tried adding Next Topic in both the source class in Swift and the playground page itself the link does not appear. NOTE: I am using Swift

Binary operator '/' cannot be applied to two (Int) operands [duplicate]

给你一囗甜甜゛ 提交于 2019-12-10 17:22:49
问题 This question already has answers here : Passing lists from one function to another in Swift (2 answers) Closed 4 years ago . I am getting a Binary operator '/' cannot be applied to two (Int) operands error when I put the following code in a Swift playground in Xcode. func sumOf(numbers: Int...) -> Int { var sum = 0 for number in numbers { sum += number } return sum } sumOf() sumOf(42, 597, 12) The above was a function calculating the total sum of any numbers. Below is a function calculating

Xcode 7 Playground execution EXC_BAD_ACCESS

雨燕双飞 提交于 2019-12-10 15:23:09
问题 Running the simplest, default code in Xcode 7 playgrounds... get the following error Playground execution failed: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x8). * thread #1: tid = 0x351bc3, 0x00000001062019ca libicucore.A.dylib`utext_clone + 22, queue = 'com.apple.main-thread', stop reason = EXC_BAD_ACCESS (code=1, address=0x8) * frame #0: 0x00000001062019ca libicucore.A.dylib`utext_clone + 22 frame #1: 0x000000010633ac42 libicucore.A.dylib`icu::RegexMatcher::reset

UITableView get titleForHeadersInSection swift

馋奶兔 提交于 2019-12-10 01:36:52
问题 I want to set the title of the header in the section of UITableView. What is the syntax in swift to set the title of the header in the section. func tableView( tableView : UITableView, titleForHeaderInSection section: Int)->String { switch(section) { case 2: return "Title 2" break default: return "" break } } func tableView (tableView:UITableView , heightForHeaderInSection section:Int)->Float { var title = tableView.titleForHeaderInSection[section]; if (title == "") { return 0.0; } return 20

How to setup ViewController in Playgrounds?

血红的双手。 提交于 2019-12-09 13:48:26
问题 In swift playgrounds how do you setup a viewcontroller and use it? I've done it with a UIView before but the UI gets cut when in a difference orientation so I want to try use a viewcontroller. I've got let view = UIViewController() but after that how do I set it up to add a background and stuff on it? 回答1: I found that to create a viewcontroller in swift playgrounds you need this code import UIKit import PlaygroundSupport class ViewController:UIViewController{ override func viewDidLoad() {

How to change the size of the Xcode Playground Simulator when using live view?

走远了吗. 提交于 2019-12-08 16:47:27
问题 I created a Swift Playground on my iPad and did all of my graphics positioning on the iPad simulator. Does anyone know how to edit the size of the Xcode playground simulator? Thanks in advance. 回答1: To change the size of a playground's liveView, set the preferredContentSize of the viewController. import PlaygroundSupport import UIKit let viewController = UIViewController() viewController.preferredContentSize = CGSize(width: 600, height: 600) PlaygroundPage.current.liveView = viewController