swift2

Swift Protocol Extensions overriding

主宰稳场 提交于 2019-12-17 02:35:09
问题 I'm experimenting with Swift protocol extensions and I found this quite confusing behaviour. Could you help me how to get the result I want? See the comments on the last 4 lines of the code. (You can copy paste it to Xcode7 playground if you want). Thank you!! //: Playground - noun: a place where people can play import UIKit protocol Color { } extension Color { var color : String { return "Default color" } } protocol RedColor: Color { } extension RedColor { var color : String { return "Red

Swift 2.1 do-try-catch not catching error

杀马特。学长 韩版系。学妹 提交于 2019-12-14 04:19:37
问题 Here's my Swift 2.1 code snippet. The error that's occurring is shown in the comments at the point where the error appears. The error shows up in the debugging panel, and the app crashes. The app never prints the line in the catch, nor does it gracefully return as expected. let audioFileURL = receivedAudio.filePathURL guard let audioFile = try? AVAudioFile(forReading: audioFileURL) else { print("file setup failed") return } let audioFileFrameCount = AVAudioFrameCount(audioFile.length)

Reloading UICollection view after data parse

一个人想着一个人 提交于 2019-12-14 03:44:09
问题 I am trying to dynamically update a uicollectionview. I used this amazing tutorial on how to create a simple uicollection. It works great when using a static array of items. My issue - I would like to have the uicollection populate with data I parsed into a new array from my db. I am not sure how to reload the uicollection after parsing my json data. UPDATED CODE WITH ANSWER: import UIKit class Books: UIViewController, UICollectionViewDelegate { @IBOutlet weak var bookscollection:

Swift: Creating a factory function that takes a class type as a parameter, and outputs a constructor of that class

僤鯓⒐⒋嵵緔 提交于 2019-12-14 03:41:48
问题 I'd like to create a factory function that takes in an class type and returns a constructor, so that I can use that constructor to create an instance of that class later. Imagine I have two classes, Apple and Orange, which are both subclasses of Fruit. They need to be initialized with an unknownNumber which I will only know about later. class Apple: Fruit { init(unknownNumber: Int) { ... } } class Orange: Fruit { init(unknownNumber: Int) { ... } } I'd like to create a factory function that

Get the start point and end point of the UIPanGestureRecognizer in Swift

只愿长相守 提交于 2019-12-14 02:37:06
问题 I'm using UIPanGestureRecognizer on a project, and I want to take the starting point and the end point. I tried to do for touchesBegin , but did not get a code that does what I need. How can I get the start point and end point of the UIPanGestureRecognizer ? 回答1: For your case, you might want to include the code inside my function into your IBAction for the panGesture. Here I've created a view, but otherwise you would just be referring to self.view instead of view . var view = UIView() func

Wrapping Alamofire calls into custom objects in Swift?

蹲街弑〆低调 提交于 2019-12-14 02:23:11
问题 I am using Alamofire to make REST calls to my server to get, add, update, and delete objects. What I'm wondering is, is it possible (and recommended) to wrap the Alamofire calls into my own custom objects (DTO), so that I can simply do things like user.delete(id) and user.add(newUser) instead of having Alamofire code all over my codebase? If so, how can I do it so I can return the success and failure handlers (the "promise" from the javascript world)? Something like this: user.add(newUser)

Random number generator function that doesn't repeat itself

偶尔善良 提交于 2019-12-14 02:20:04
问题 Does swift by default has a random number generator that returns the same number only once? For example it picks a number in range 1,2,3,4,5. Return that random number (lets say 3). In the next loop, it picks only from 1,2,4,5 and so on. EDIT: This is what I ended up using. It returns an array of integers from 0-5 and was tested in playground. Note that this is meant to be used when you are picking from a large set of Integers and not just 6. func generateRandomArray() -> [Int]{ var

How do I iterate through a [String:[String]] Dictionary in Swift

爷,独闯天下 提交于 2019-12-13 23:36:18
问题 I have a dictionary. var params: [String: [String]] = [:] I assign an array to the first key and the first key only. Now print params prints : ["names" : ["jack", "joe", "jill"]] How do I iterate through this array at this given key so that I could loop through and print jack, joe, and jill? 回答1: for (key, names) in params { for name in names { print("\(name)) } } 回答2: var params: [String: [String]] = ["names" : ["jack", "joe", "jill"]] for key in params.keys { NSLog("%@", key) let list =

Cancel and Reschedule a Local Notification(Swift)

守給你的承諾、 提交于 2019-12-13 21:24:15
问题 I'm trying to show a local notification with a time interval of 1 day (24 hours) and its working fine but now I want to cancel notification for a specific day. I tried(today) this but it didn't seem to work. My notification is still appearing this is what I have done for cancelling the notification: let appNotification = appDelegate!.notification UIApplication.sharedApplication().cancelLocalNotification(appNotification) This is how am scheduling the notification in appDelegate : //scheduling

Slide finger across 3 buttons - Xcode & Swift

扶醉桌前 提交于 2019-12-13 20:07:14
问题 I have 3 buttons in my UI that ultimately will perform similar to keys on a piano. Either of the 3 buttons may get tapped to perform that particular buttons actions, but the user can 'slide' their finger off onto the next button to perform button 2's action. I did some searching and it looks like a touchesBegan method detecting the location of the tap is best. My attempt is below: @IBOutlet weak var button1: UIButton! @IBOutlet weak var button2: UIButton! @IBOutlet weak var button3: UIButton!