swift-playground

NSJSONSerialization not working as expected in a Playground

☆樱花仙子☆ 提交于 2019-11-29 16:13:16
I have to get football game schedule via JSON and extract date of each game of one of the specific universities. I tried: let url = NSURL(string: "SCHOOL URL") let request = NSURLRequest(URL: url!) let session = NSURLSession.sharedSession() let task = session.dataTaskWithRequest(request){ (data, response, error) -> Void in do{ let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments) if let schedule = jsonData["schedule"] as? [[String: AnyObject]]{ for game in schedule{ if let date = game["date"] as? String{ print("\(date)"); } } } } catch let error as NSError{

NSTimer.scheduledTimerWithTimeInterval in Swift Playground

南楼画角 提交于 2019-11-29 14:02:40
All the examples I've seen on using the "NSTimer.scheduledTimerWithTimeInterval" within Swift show using the "target: self" parameter, but unfortunately this doesn't work in Swift Playgrounds directly. Playground execution failed: <EXPR>:42:13: error: use of unresolved identifier 'self' target: self, Here's an example referenced above that results in the error: func printFrom1To1000() { for counter in 0...1000 { var a = counter } } var timer = NSTimer.scheduledTimerWithTimeInterval(0, target: self, selector: Selector("printFrom1To1000"), userInfo: nil, repeats: false ) timer.fire() You really

XML parsing in swift

假如想象 提交于 2019-11-29 13:59:28
Can some body help me why the below code is not working.. I am testing it in Xcode.1 Playground let url:NSURL! = NSURL(fileURLWithPath:"file:///Users/sss/Documents/app.xml") var xml = NSXMLParser(contentsOfURL: url) xml?.parse() Playgrounds are sandboxed, so you won't be able to just grab files from anywhere in your user folder. Here's how to add that file to your playground to make it accessible: Find your ".playground" file in the Finder Right click and choose "Show Package Contents" You should see "timeline.xctimeline", "contents.xcplayground", and "section-1.swift" Make a new folder called

Sequence of elements, but “join is unavailable: call the 'joinWithSeparator()'” error

孤街浪徒 提交于 2019-11-29 11:13:46
Why do I get the error "join is unavailable: call the joinWithSeparator() " at line 16 (the last line below) when I try to run it on playground? And, how can I fix it? class Person { var firstName: String? var lastName: String? let gender = "female" func fullName() -> String { var parts: [String] = [] if let firstName = self.firstName { parts += [firstName] } if let lastName = self.lastName { parts += [lastName] } return " ".join(parts) } } The error message tells you what the problem is, and it tells you how to fix it. Read the error message! Do what the error message says! return parts

Swift iOS playground: Error sending deferral props

本秂侑毒 提交于 2019-11-29 11:13:42
问题 With an iOS playground set up as simply as this: import UIKit import SpriteKit import XCPlayground let s = CGSize(width: 300, height: 300) let f = CGRect(origin: CGPointZero, size: s) let view = SKView(frame: f) let scene = SKScene(size: s) scene.backgroundColor = SKColor.redColor() view.presentScene(scene) XCPShowView("main view", view) I'm getting this in the console: 2014-09-04 17:02:13.358 SpriteKitBETA7[2009:20695] Error sending deferral props: 0x10000003 There IS a "main view" box in

How do you instantiate a Storyboard from a file within an iOS playground?

亡梦爱人 提交于 2019-11-29 07:19:23
问题 Let's assume you've copied a Main.storyboard from an Xcode 6 project into a standalone playground's Resources directory. How can you instantiate a UIStoryboard using the Main.storyboard file? Trying to use the default via nil doesn't work: let storyboard = UIStoryboard(name: "Main", bundle: nil) Nor does explicitly using the main bundle: let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle()) Even if the playground is part of the storyboard's Xcode project, I receive the

Error running playground. Unable to find suitable target device. (iOS Swift)

↘锁芯ラ 提交于 2019-11-29 07:07:40
问题 I created a new project in XCode. File > New > Playground (option+shift+cmd+N) I restarted my XCode and also rebooted my system, but still same error. 回答1: Try navigating to the Xcode Menu Window >> devices (shift+cmd+2) a new window will pop up. On the bottom left, add a new simulator, specifically one running on iOS 8.0 to the existing list. Restart Xcode. 回答2: From the developer forums, this worked for me: Quit Xcode Delete the folder /Users/<username>/Library/Developer/CoreSimulator

Array of functions in Swift

守給你的承諾、 提交于 2019-11-29 06:04:54
问题 How can I store an array of functions to callback later in an array like in JavaScript? Any and AnyObject type cannot hold functions with different types of method signatures. 回答1: You can use an enum to put various functions into the Array and then extract the functions with a switch. enum MyFuncs { case Arity0 ( Void -> Void ) case Arity2 ( (Int, String) -> Void) } func someFunc(n:Int, S:String) { } func boringFunc() {} var funcs = Array<MyFuncs>() funcs.append(MyFuncs.Arity0(boringFunc))

Declarations in extensions cannot override yet error in Swift 4

廉价感情. 提交于 2019-11-29 04:25:52
问题 I have an extension: public extension UIWindow { override public func topMostController()->UIViewController? { ... } } but for my topMostController I get the next error: Declarations in extensions cannot override yet error It works well for Swift 3.1, but for Swift 4 I get this error. How can it be fixed? What did they change in Swift 4? 回答1: It will work if you make the base implementation @objc . See Hamish's answer for a detailed explanation about the internals. Overriding methods declared

Using Alamofire within a Playground

那年仲夏 提交于 2019-11-29 01:41:20
I'm new to iOS development and using Xcode and I'm having trouble getting Alamofire to work within a Playground. There's some functionality I'd like to test out for proof of concept but the library is not linked to the Playground and I've tried getting it to play nicely. I have Alamofire set up to work within an iOS (not in a Playground) project before the installation instructions in the Github Alamofire repo were recently updated. Any suggestions on how to get Alamofire to import properly in the Playground? Canucklesandwich Apple provides excellent step-by-step instruction to do so here: