swift2

Turning my code into a better Swift like answer for a FizzBuzz response [closed]

梦想与她 提交于 2019-12-21 21:26:37
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago . I was searching the internet last night while working on some programming, and I noticed that there was this interesting test that employers sometimes use for testing programmers to see if they can actually apply code to a real world problem. It is referred to as the FizzBuzz

Adding An Item To An NSSet For A Core Data One To Many Relationship

て烟熏妆下的殇ゞ 提交于 2019-12-21 20:00:30
问题 I have a core data relationship where one entity holds many of another entity. As far as I am aware each instance of the many class is held inside an NSSet? inside the one class. (?) My question is - what is the best way to add items to this set? I figure this must be a very common problem - but I cannot seem to find an easy method. This is my attempt: (This is all taken from the one class) static var timeSlotItems: NSSet? //The Set that holds the many? ... static func saveTimeSlot(timeSlot:

XCode 7 print keys and values of Dictionary

纵然是瞬间 提交于 2019-12-21 17:51:21
问题 Print description in xcode 7 is giving memory addresses similar to below. Tried all the options, but getting output like this. Please let me know how to print the exact value of a dictionary. ▿ 3 elements ▿ [0] : 2 elements - .0 : Output ▿ .1 : 3 elements ▿ [0] : 2 elements - .0 : type - .1 : Output2 { ... } ▿ [1] : 2 elements - .0 : version - .1 : 1.0 ▿ [2] : 2 elements - .0 : content ▿ .1 : 2 elements Not sure why Apple is 100% concentrating on console window.. there is change from XCode 5

Introspection and iteration on an Enum

送分小仙女□ 提交于 2019-12-21 17:32:40
问题 Is it possible to programatically find out how many "cases" an Enum has in Swift 2 and iterate over them? This code doesn't compile, but it gives yo an idea of what I'm trying to achieve: enum HeaderStyles{ case h1 case h2 case h3 } for item in HeaderStyles{ print(item) } 回答1: You can write a generic struct that provide that iteration ability. In example below the enum raw values must start with 0 (it does it by default) and have no gaps between raw values (can't have raw values such as 0,1,2

UITextField doesn't end editing when Button clicked( delegate textFieldDidEndEditing )

主宰稳场 提交于 2019-12-21 17:18:40
问题 I have two textFields on the screen and a Submit button . User inputs details in first textField and then the second one. My requirement is to end the editing when Submit button is clicked and print the user inputs in these textFields .I am having issues printing the second textField's value, as the editing never seems to end when the user clicks the Submit button . Here is my code. Appreciate your help on this issue (I have added the textfield delegate) import UIKit class ViewController:

Swift 2.0 code for Facebook Friend Invite

老子叫甜甜 提交于 2019-12-21 17:15:35
问题 I've been looking for an equivalent Swift code sample for Facebook friend invite for iOS apps. But I can't find them. I understand that there is the Objective-C version on Facebook page https://developers.facebook.com/docs/app-invites/ios. However, because I started off with Swift, I find it difficult to translate. Could someone point me to a source? Thank you. 回答1: the code working : -In viewDidLoad : let content = FBSDKAppInviteContent() content.appLinkURL = NSURL(string: "https://test

Guard when setting multiple class properties in Swift 2

六月ゝ 毕业季﹏ 提交于 2019-12-21 16:57:16
问题 It's trivial enough to do something like this: class Collection { init(json: [String: AnyObject]){ guard let id = json["id"] as? Int, name = json["name"] as? String else { print("Oh noes, bad JSON!") return } } } In that case we were using let to initialize local variables. However, modifying it to use class properties causes it to fail: class Collection { let id: Int let name: String init(json: [String: AnyObject]){ guard id = json["id"] as? Int, name = json["name"] as? String else { print(

Passing arguments to #selector method in swift [duplicate]

孤人 提交于 2019-12-21 16:44:28
问题 This question already has answers here : Pass extra argument for UItapgestureRecognizer with selector (3 answers) Closed 3 years ago . I want to pass multiple argument to a function while I click an image. Here is my code var param1 = 120 var param2 = "hello" var param3 = "world" let image: UIImage = UIImage(named: "imageName")! bgImage = UIImageView(image: image) let singleTap = UITapGestureRecognizer(target: self, action:#selector(WelcomeViewController.tapDetected(_:secondParam:thirdParam:)

Swift, Custom UIButton does not work when click

回眸只為那壹抹淺笑 提交于 2019-12-21 12:39:58
问题 I have a custom UIButton created with xib file. When i use my custom button on my view, it does not work when i press to it. My RoundBtn.swift file: import UIKit @IBDesignable class RoundBtn: UIButton { var nibName = "RoundBtn" @IBOutlet weak var btnImageView: UIImageView! @IBOutlet weak var btnLabel: UILabel! @IBInspectable var image: UIImage? { get { return btnImageView.image } set(image) { btnImageView.image = image } } @IBInspectable var label: String? { get { return btnLabel.text } set

countForFetchRequest in Swift 2.0

萝らか妹 提交于 2019-12-21 09:35:21
问题 I am trying to use the countForFetchRequest method on a managed object context in Swift 2.0. I note that the error handling for executeFetchRequest has been changed across to the new do-try-catch syntax: func executeFetchRequest(_ request: NSFetchRequest) throws -> [AnyObject] but the countForFetchRequest method still uses the legacy error pointer: func countForFetchRequest(_ request: NSFetchRequest, error error: NSErrorPointer) -> Int ...and I am having a bit of trouble figuring out how to