swift-playground

Making my function calculate average of array Swift

你。 提交于 2019-11-26 18:55:25
I want my function to calculate the average of my Double type array. The array is called "votes". For now, I have 10 numbers. When I call the average function to get the average of the array votes, it doesn't work. Here's my code: var votes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] func average(nums: Double...) -> Double { var total = 0.0 for vote in votes { total += vote } let votesTotal = Double(votes.count) var average = total/votesTotal return average } average[votes] How do I call the average here to get the average? You should use the reduce() method to sum your array as follow: Xcode 10 • Swift

How to print to console using swift playground?

主宰稳场 提交于 2019-11-26 18:43:36
I have been following the Apple Guide for their new language swift, but I don't understand why the bar on the right is only showing "Hello, playground" and not "Hello, world". Can someone explain why the println isn't being printed on the right? // Playground - noun: a place where people can play import Cocoa var str = "Hello, playground" println("Hello, world"); In Xcode 6.3 and later (including Xcode 7 and 8), console output appears in the Debug area at the bottom of the playground window (similar to where it appears in a project). To show it: Menu: View > Debug Area > Show Debug Area (⌘⇧Y)

Swift playground - How to convert a string with comma to a string with decimal

痞子三分冷 提交于 2019-11-26 18:28:37
问题 I'm new in the Swift world. How can I converting a String with a comma to a String with a decimal? The code work's fine with a dot (.) The problem is when I'm using a comma (,) ... with: var price The origin of the problem is the Decimal french keyboard use a comma (,) instead of a dot (.) Don't know exactly how to use NSNumberFormatter or generatesDecimalNumbers if it's the key. There's probebly more than one options. //The answer change if "2,25" or "2.25" is used. var price : String = "2

Convert Character to Int in Swift 2.0

夙愿已清 提交于 2019-11-26 18:13:00
问题 I just want to convert a character into an Int . This should be simple. But I haven't found the previous answers helpful. There is always some error. Perhaps it is because I'm trying it in Swift 2.0. for i in (unsolved.characters) { fileLines += String(i).toInt() print(i) } 回答1: In Swift 2.0, toInt() , etc., have been replaced with initializers. (In this case, Int(someString) .) Because not all strings can be converted to ints, this initializer is failable , which means it returns an optional

My Swift 4 UIScrollView with autolayout constraints is not scrolling

二次信任 提交于 2019-11-26 17:48:41
问题 I have created a small demo playground to get this working before adding the view to my app. I have a scroll view that is going to contain a number of buttons to scroll through horizontally. I know that these buttons need to go into a container view within the scroll view and have created this as well. I was initially using autolayout constraints to create all of this, but have now tried using constants to ensure the content view is bigger than the scroll view. However, the buttons still will

Working with Live Photos in Playground

纵饮孤独 提交于 2019-11-26 17:43:58
问题 I've done a fair amount of searching the web, but I'm currently attempting to work with "Live Photos" in Playground. I'm aware of the framework (PHLivePhoto), I just have no clue if working with them in Playground is possible due to that fact that there's not much to "import" as there doesn't seem to be any "Live Photos" available for download online. Any ideas? 回答1: It's possible to make and view a PHLivePhoto in the Playground from the elements of an actual Live Photo . In OS X's Photos app

How to import own classes from your own project into a Playground

瘦欲@ 提交于 2019-11-26 15:49:01
Assume a setup like this: You have an Xcode 6 project, where you've implemented your own classes (say MyView and MyViewController) with both Objective-C and Swift You have added a Playground into your project In the Playground, it's possible to import modules (frameworks) like UIKit with the import keyword. How do you enable access to the project's other classes from the Playground? Just trying to access project classes directly results with an error message: Use of unresolved identifier 'MyView' Rick Ballard As of Xcode 6.0 Beta 5, it is now possible to import your own frameworks into a

Swift Playground - Files are not readable

寵の児 提交于 2019-11-26 14:35:37
问题 Files are not readable in Swift Playground . How to make files readable? Same code runs well on Xcode terminal app, but fails on Swift Playground. Demo code below. import Foundation println("Hello, World!") var fname:String = "/Users/holyfield/Desktop/com.apple.IconComposer.plist" var fm:NSFileManager = NSFileManager.defaultManager() if(fm.fileExistsAtPath(fname)){ println("File Exists") if(fm.isReadableFileAtPath(fname)){ println("File is readable") var fd:NSData? = NSData(contentsOfFile:

what are the rules for spaces in swift

老子叫甜甜 提交于 2019-11-26 11:33:00
问题 I practicing in swift\'s playground and I couldn\'t figure out why swift is too specific about where programmer should provide spaces and where not. I asked this question on many sites and chatrooms but didn\'t got any answer. var j: Int = 34 // Right var j:Int=23 //Wrong Also, In class self.variable-= 5 //Wrong. Error: Consecutive statements must be followed by ; self.variable-=5 // Right self.variable -= 5 // Right ; Even this \":\" creates some issues with spaces sometimes. I think spaces

Iterate through a String Swift 2.0

為{幸葍}努か 提交于 2019-11-26 11:15:51
问题 I am trying to do a very simple piece of code in Swift playgrounds. var word = \"Zebra\" for i in word { print(i) } However, I always get an error on line 3. \'String\' does not have a member named \'Generator\' Any ideas on why this doesn\'t work? Note: I am working in Xcode 7, with Swift 2.0 (Strings and Characters). 回答1: As of Swift 2, String doesn't conform to SequenceType . However, you can use the characters property on String . characters returns a String.CharacterView which conforms