swift-playground

Add a method to Card that creates a full deck of cards, with one card of each combination of rank and suit

人走茶凉 提交于 2019-11-28 20:42:27
So I have been doing the experiments that are in the Apple Swift Book. I have been able to do all of them, except for this one so far. Below is what I have tried, but I can't figure out how to get it working. Add a method to Card that creates a full deck of cards, with one card of each combination of rank and suit. // Playground - noun: a place where people can play enum Rank: Int { case Ace = 1 case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten case Jack, Queen, King func simpleDescription() -> String { switch self { case .Ace: return "ace" case .Jack: return "jack" case .Queen: return

Could not find an overload for '/' that accepts the supplied arguments

て烟熏妆下的殇ゞ 提交于 2019-11-28 17:27:44
// Playground - noun: a place where people can play func getAverage(numbers: Int...) -> Double{ var total = 0 var average:Double = 0 for number in numbers{ total = total + number } average = total / numbers.count return average } getAverage(3, 6) I get an error on average = total / numbers.count Could not find an overload for '/' that accepts the supplied arguments I tried to fix by doing: average = Double(total/numbers.count) but then the getAverage was set to 4 instead of 4.5 Jean-Philippe Pellet There are no such implicit conversions in Swift, so you'll have to explicitly convert that

How do I make an UIImage/-View with rounded corners CGRect (Swift)

前提是你 提交于 2019-11-28 17:09:50
How do I make a UIImageView with rounded corners on a Swift iOS Playground? Inside it needs to be filled with a color. let imageView = UIImageView(frame: CGRectMake(0, 0, 100, 100)) imageView.backgroundColor = UIColor.redColor() imageView.layer.cornerRadius = 8.0 imageView.clipsToBounds = true Result: For rounded circle image frame in swift, what it worked for me was: self.profileImageView.image = UIImage(named:"profileUser") self.profileImageView.layer.cornerRadius = self.profileImageView.frame.size.width / 2 self.profileImageView.clipsToBounds = true And for adding a shadow: self

How to return a first word from a string in Swift?

和自甴很熟 提交于 2019-11-28 13:44:43
If we have, for example, situation like this: var myString = "Today was a good day" What is the best way to return the first word, which is "Today"? I think mapping should be applied, but not sure how. Thanks. The simplest way I can think of is Swift 3 let string = "hello world" let firstWord = string.components(separatedBy: " ").first Swift 2.2 let string = "hello world" let firstWord = string.componentsSeparatedByString(" ").first and if you think you need to use it a lot in your code, make it as an extension extension String { func firstWord() -> String? { return self.components(separatedBy

Fixing low FPS in Swift Playground

喜你入骨 提交于 2019-11-28 13:43:01
My SpriteKit playground book averaged 15 FPS on my MacBook Pro. Do playgrounds run slower than iOS device simulations? If I run the same playground book on my iPad Pro, will the FPS limitation be similar? Will other apps opened on my computer limit the speed of playgrounds? EDIT: Moving code such as subclasses and extensions to auxiliary code in the "Sources" folder of the playground book allow the simulation to run quicker because the code only compiles once. On the Mac, Xcode's "Playgrounds" are super useful for quick experiments but, due to their nature, terribly slow for "real" tasks. If

Why does Swift playground shows wrong number of executions?

回眸只為那壹抹淺笑 提交于 2019-11-28 12:50:13
I am using a completion handler to sum up numbers. What I don't understand is if I break my code in 2 lines, the number of executions would change from 6 to 7!! WHY? func summer (from : Int, to: Int, handler: (Int) -> (Int)) -> Int { var sum = 0 for i in from...to { sum += handler(i) } return sum } summer(1, to:6){ //Shows '21' return $0} // shows '(6 times)' // Same code, but in 1 line summer(1, to:6){return $0} // shows '(7 times)' IMAGE Fluidity It's showing how many times a function / expression is being called on that line: since the calling expression (summer()) is on the same line, it

Access properties via subscripting in Swift

笑着哭i 提交于 2019-11-28 11:57:13
I have a custom class in Swift and I'd like to use subscripting to access its properties, is this possible? What I want is something like this: class User { var name: String var title: String subscript(key: String) -> String { // Something here return // Return the property that matches the key… } init(name: String, title: String) { self.name = name self.title = title } } myUser = User(name: "Bob", title: "Superboss") myUser["name"] // "Bob" Update: The reason why I'm looking for this is that I'm using GRMustache to render from HTML templates. I'd like to be able to just pass my model object

Can I import my project code into the Swift REPL?

我的未来我决定 提交于 2019-11-28 10:05:29
The Swift REPL is great, but it would be even better if I could import the classes from an Xcode project. I tried switching to my project directory and running $ swift > import ProjectName but I got: error: no such module 'ProjectName' Is it possible to do this? The Swift REPL includes a number of different options. Use swift -help to see them. For your case, if you've defined ProjectName as a framework target and in the target you've declared 'Defines Module' then you can access it with: $ swift -F <install path with subdirectory ProjectName.framework> > import ProjectName Here is an example:

Xcode: Any way to refresh/re-run the playground?

旧街凉风 提交于 2019-11-28 09:35:11
The Playground in Xcode automatically updates as you type, but I can't figure out how to get the Playground to "re-compile". In many cases this wouldn't matter, but if you're writing code that generates or uses random values it can be useful to run it a few times to make sure it's working. Is there any way to get the playground to reset / refresh / re-run? Seen a few questions asking how to stop the Playground from auto-updating, but nothing for the opposite. Easiest way to do this seems to be to just edit the code (add and remove a space), or put in some kind of a loop... Just wondering if

Swift Update Label (with HTML content) takes 1min

馋奶兔 提交于 2019-11-28 08:41:46
I got a small problem, let me start with the code class ViewController: UIViewController { @IBOutlet weak var LBoutput: UILabel! @IBAction func BTclick(sender: AnyObject) { var url = NSURL(string: "http://google.com") println("test0") let getdata = NSURLSession.sharedSession().dataTaskWithURL(url){(data ,response , error) in var htmlContent = NSString(data: data, encoding: NSUTF8StringEncoding) println("test1") println("test2") self.LBoutput.text = "test6" } println("test3") getdata.resume() println("test4") LBoutput.text = "test5" } This codes give me a output in the console of test0 test3