FAQ Database Discussion Community


Simple Swift Array Extension

arrays,swift,swift-playground
Trying to extend Array type to use binary sorting to insert elements in order. Here's my playground code: extension Array { func insertionIndexOf(elem: T , isOrderedBefore: (T, T) -> Bool) -> Int { var lo = 0 var hi = self.count - 1 while lo <= hi { let mid...

Why custom delegate in iOS is not called

ios,swift,delegates,swift-playground
I am trying to create a custom delegate using playground in swift. However the doSomething method is not being called through callback. It seems that delegate?.doSomething() does not fire to the XYZ class doSomething method. Thanks in advance! import UIKit @objc protocol RequestDelegate { func doSomething(); optional func requestPrinting(item :...

Importing XCPlayground module in Swift REPL

swift,swift-playground
I'm on OS X 10.10.2 and Xcode 6.1.1. I'd like to import the XCPlayground module so I can test using the NSURLSession module (make an HTTP GET request). When I run the Swift REPL via: swift then run import XCPlayground I get the following error: repl.swift:1:8: error: no such module...

Making HTTP calls ( GET, POST etc ) from Swift ( iOS)

ios,iphone,swift,swift-playground
I am fairly new to swift programming language and I am trying to find a way to make a HTTP/API call from swift. I know how to do it exactly in Javascript because I have worked with it before quite a lot. But I can't seem to figure out in...

How to Read and Write data to a text file in Swift using playground? [duplicate]

swift,file-io,swift-playground
This question already has an answer here: Swift Playground - Files are not readable 1 answer I read through these SO links for the answer 1. Read a file/URL line-by-line in Swift 2. Read and write data from text file Link 2 Provided me the solution but the problem...

Using NSArray as function parameter

swift,swift-playground
I'm trying to add all elements of one NSArray using the NSNumber in for. Why this code doesn't work in playground example? func sumaEnteros(enteros : NSArray) -> Int { var result = 0 for NSNumber i in enteros{ result += enteros.indexOfObject(i) } return result } Param "enteros" is marked as...

Swift optional values during initialization preventing default initializer inheritance

swift,initialization,constants,optional,swift-playground
In Swift: 1) If you provide a default value for all of the stored properties in a class, then you inherit the default initializer, ie - init(). -- AND -- 2) A property of any optional type defaults to the value of nil, ie - var shouldBeNill: String? //should initially...

Multiplication of Integer by double and storing result as a double

ios,swift,swift-playground
Out of plain curiosity, in swift: var a:Double = 3.5 var b:Int = 4 var c = a * Double(b) var d:Double = a * b Why is d not valid but c is? Since I'm specifying that d is a double, shouldn't the compiler detect that b should be...

Swift, split a double by dot to two numbers

swift,swift-playground
So I am trying to split a number in swift, I have tried searching for this on the internet but have had no success. So first I will start with a number like: var number = 34.55 And from this number, I want to create two separate number by splitting...

learning swift by GuidedTour

swift-playground
in this example of GuidedTour of Swift by https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/ var optionalString: String? = "Hello" optionalString == nil var optionalName: String? = "John Appleseed" var greeting = "Hello!" if let name = optionalName { greeting = "Hello, \( name)" } I can not do this experiment, because of error Change optionalName...

In Xcode playground, control the string representation in the right side column?

swift,xcode6,swift-playground,xcode6.3
I thought the Printable protocol would do it, but it doesn't. Is there another protocol? I want it to show the 3 numbers, not "C._GLKVector3" ...

How to cast numeric types?

ios,swift,casting,swift-playground,typecasting-operator
I am using Xcode playground to downcast in swift. Typecasting would normally allow me to convert a type to derived type using As operator in swift. But it gives me error while i try to typecast var a as Double,String. Thanks in advance!! var a = 1 var b =...

Make “int” and “float” work in swift playground

objective-c,swift,swift-playground
so i am just starting to teach myself a bit ios and am an absolute amateur (please dont be too harsh and forgive questions, of which to you the answers might be obvious). so i wanted to get this following code to work: var int bildBreite; var float bildHoehe, bildFlaeche;...

Does not have a member named ( Swift )

swift,swift-playground,swift-extensions
I have two labels (CountryCode, CountryName) in prototype cells, and I created a class for them called " CountryCell " and I created the TableViewController class called " CountriesVC " I made the labels' outlets in the CountryCell but I want to use them in CountriesVC and when I do...

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

string,comma,swift-playground
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...

“Playground execution failed” when using XCPShowView(identifier: view:)

ios,uiview,ios8.1,swift-playground
When using XCPShowView the Swift Playground gives me Playground execution failed: error: Couldn't lookup symbols: __TF12XCPlayGround11XCPShowViewFTSSCSo6UIView_T_ I use xcrun swift-demangle __TF12XCPlayGround11XCPShowViewFTSSCSo6UIView_T_ to see what's the demangled name and it's XCPlayGround.XCPShowView (Swift.String,ObjectiveC.UIView) -> () (Though you can see what's the demangled name just by reading __TF12XCPlayGround11XCPShowViewFTSSCSo6UIView_T_) The code is here import...

AVAudioFile in Swift Playground results in “error -54”

swift,core-audio,swift-playground,avaudiofile
I decided to have a play with AVAudioPlayer in a Swift Playground. The following code works fine in a normal Swift project, but returns the following error when running it in a Playground (in the assistant editor): 2015-05-12 00:08:04.374 AVAudioFile[2481:141158] 00:08:04.374 ERROR: AVAudioFile.mm:266: AVAudioFileImpl: error -54 Here is the code:...

Swift Version 1.2 not available in the playgrounds of Xcode 6.3?

xcode,swift,swift-playground
One of the new features on Xcode 6.3 Beta (6D520o) supposed to be a changed behavior of the let statement. With Swift 1.2 you should be able to assign a value to a let constant later on. Not only with the declaration. As long as you initialized a value before...

Call Slack Webincoming hook in Swift but get “interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)”

ios,json,swift,swift-playground,slack-api
I've use Swift to post something to Slack use Webhook as an POST request, but get an error like interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) in the line of var request = .... Can anyone tell me why I get such an error? Thanks!! :D ("Webhook URL here" refers to a...

Embed Xcode Playground within an application

objective-c,xcode,cocoa,swift,swift-playground
I want to make a Cocoa OS X application where users will be able to input and execute Swift language code within a window in my application. My first thought was to make my own basic IDE, and execute compilation and executing with system calls; however, I'd really like to...

Swift: “Cannot invoke subscript” : setting an objects members up by handing it a class's array at an index

swift,swift-playground
I'm having an issue while programming in Swift. After i create an Instance of the SchedulePatientsTest class I begin to get this error: Cannot invoke 'subscript' with an argument list of type '(name:$T4, email: $T9, ssn: $T14, dob: $T19)' Since I'm quite new to Swift I'm having trouble debugging it...

Why does the following swift code gives error?

ios,swift,swift-playground
The array does not append with a custom class object // Create an array with String and Double var shoppingList = ["Eggs", 2.0] // Append array with string object shoppingList += ["Milk"] // Declare an example class class Foo { var name : String? func Foo() { name = "Default...

NSInvalidArgumentException : A Node can't parent itself

swift,swift-playground
I would like to know what it means when we get an error like the 'NSInvalidArgumentException', reason: 'A Node can't parent itself' : 2015-03-04 01:51:33.421 FlappyPOOSwift[1789:155918] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'A Node can't parent itself: <SKSpriteNode> name:'(null)' texture:[<SKTexture> 'pipeDown' (30 x 160)] position:{0, 851} size:{60,...

Understanding outputs in Playground

ios,swift,xcode6.1,swift-playground
Why does the following code outputs as follow in Playground... class Vehicle{ func run(){ println("Running")//outputs--------- Running } } var jetta = Vehicle()// outputs ------------ Vehicle jetta.run()// outputs ---------------------- Vehicle (why not running?) Shouldn't the output for the last call be "Running"? When I try the same code on swiftstub.com or...

Making my function calculate average of array SWIFT

arrays,function,swift,swift-playground
I'm new to coding so bear with me! I want my function, that calculates the average, to calculate the average of my array. The array is called "votes" and I want to calculate the average of the whatever is inside the array. For now, just 10 numbers. When I call...

Scene Editor: can't specify texture name without @2x

ios,sprite-kit,swift-playground,sktexture
I am setting up a test Sprite Kit Scene file (*.sks) that I plan to use with both the iPhone6 and iPhone6 Plus. I have two versions of my Spaceman art work called: [email protected] (50 x 50 pixels, 25 x 25 points) [email protected] (75 x 75 pixels, 25 x 25...

Type '()" does not confirm to protocol: 'BooleanType'

swift,swift-playground
Having difficulty, ound some articles / questions asking this but it wasn't exactly what it was. var catMoves = 0 let maxCatMoves = 8 func catOutOfMoves() { if catMoves = maxCatMoves { var aliveCat = 0 }} //do something else {//do something else} Thanks, much appreciated. ...

String indices in Swift 2

xcode,swift,swift-playground,swift2
I decided to learn Swift and I decided to start with Swift 2 right away. So here is an very basic example that's similar to one of the examples from Apple's own e-book about Swift let greeting = "Guten Tag" for index in indices(greeting) { print(greeting[index]) } I tried this...

Optionals in Swift, why this simple code doesn't print optional?

ios,swift,optional,swift-playground
var shoppingList = ["item1": "bread", "item2": "milk" ] if let oldItem = shoppingList.updateValue("honey", forKey: "item2") { println("old value was \(oldItem)") } // This prints out "old value was milk" However, if I do this var shoppingList = ["item1": "bread", "item2": "milk" ] let oldItem = shoppingList.updateValue("honey", forKey: "item2") println("old value...

Iterate through a String Swift 2.0

swift,swift-playground,swift2,xcode7
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:...

How writeToFile in a .playground?

swift,save,plist,swift-playground
I have edited a .plist file. How I understood it: because of sandboxing it is only possible to read files inside the .playgrounds file in a Resources directory. But how is it possible to write the edited file to this folder? // read file let xmlPath: String = NSBundle.mainBundle().pathForResource("Settings", ofType:...

Convert Character to Int in Swift 2.0

swift,int,character,swift-playground,swift2
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) } ...

How can I display views using autolayout constraints in Xcode playground?

ios,xcode,swift,autolayout,swift-playground
I am trying to display views configured with autolayout constraints in XCode playground, but it doesn't seem to work. It's like playground ignores the constraints completely, and I can't find information about this issue anywhere. Here's the code I tried: let view = UIView() view.frame = CGRectMake(0, 0, 400, 200)...

NSTask in playground

osx,swift,nstask,swift-playground
I'm wondering if NSTask is not available in playgrounds. This does not compile import Foundation let task = NSTask() Does anyone know if that's not (yet) possible or am I missing something?...

Blend mode kCGBlendModeMultiply is not correctly multiplying an image with a color

ios,osx,swift,core-graphics,swift-playground
Goal: To tint an image by multiplying it with a color. Assumptions: According to Apple's documentation, the blend mode kCGBlendModeMultiply "multiplies the source image samples with the background image samples." What I understand from this is: R = S * D for R, G, B and A. So if one...

Empty Class in Swift Playground Gives __lldb_expr_ Error

swift,ios8,xcode6,swift-playground
Knocking up an empty class within a Swift Playground gives an error __lldb_expr_ //: Playground - noun: a place where people can play import UIKit class FooBar { } let foo = FooBar() See attached screenshot. This occurs on Xcode Version 6.3.1 (6D1002). I have also tried with the latest...

Extra argument in call error mystery

ios,swift,swift-playground
I had some older Swift code that used to compile and work where I was using the .append to build out a data structure dynamically. After upgrading to a few compiler versions newer I am getting the dreaded "Extra Argument ' ' in call" error. I reduced the code down...