swift3

How can I write a function that takes generic type arguments, but returns a different type based on what the generic object's type is? [duplicate]

廉价感情. 提交于 2019-12-12 03:44:16
问题 This question already has answers here : How can I handle different types using generic type in swift? (2 answers) Closed 3 years ago . I'm trying to write a basic interpolation function in swift3. I get a lot of errors, though. This is obviously not the right way to use generics, but maybe I have a fundamental misunderstanding of their application? class func interpolate<T>(from: T, to: T, progress: CGFloat) -> T { // Safety assert(progress >= 0 && progress <= 1, "Invalid progress value: \

Elegant Swift way to handle negative index in for loop

孤街醉人 提交于 2019-12-12 03:43:27
问题 I'm new to Swift and trying to find an elegant way to handle a for loop variable that can be negative. func funForLoops(_ loop:Int) { for i in 0..<loop { print("Hello \(i)") } } funForLoops(1) // prints Hello 0 funForLoops(0) // doesn't execute funForLoops(-1) // runtime error "fatal error: Can't form Range with upperBound < lowerBound" Is there a simpler way to check this than this: if (loop >= 0) { for i in 0..<loop { print("Hello \(i)") } } Or this: for i in 0..<(loop >= 0 ? loop : 0) {

Undo manager for tableView row delete

江枫思渺然 提交于 2019-12-12 03:43:25
问题 Is it possible to implement an undo manager when you delete a row in a tableView ? I have read about undo manager and you have to set the viewController as becomeFirstResponder() . The problem is that I am using a tableView inside a collectionViewCell How shall I proceed with registering an undo? I tried the following and it doesn't seem to work @IBAction func tableButtonUndo(_ sender: Any) { print("undo working") undoItem() } func tableView(_ tableView: UITableView, didSelectRowAt indexPath:

NSURL to NSDATA or Data is always nil

夙愿已清 提交于 2019-12-12 03:43:21
问题 I am trying to upload a video to a server file through Alamofire but I couldn't get the "data" going to be passed..its always nil var videoURL = NSURL(string: "") //returns Optional(file:///private/var/mobile/Containers/Data/Application/1FB40086-228B-4011-A9D4-7874E2EEF9F4/tmp/4A6AAD76-B899-4B67-8E96-925DA4AE9E93.mov) let videodata = NSData(contentsOfFile: (videoURL?.absoluteString)!) //nil let url = NSURL(fileURLWithPath: (videoURL?.absoluteString)!) let videodata = NSData(contentsOf: url as

Why is compiler forcing me to use sorted(by:) in Swift 3

六眼飞鱼酱① 提交于 2019-12-12 03:43:20
问题 I am using Swift 3. I have a dictionary of arrays, and I am trying to sort the arrays in place using a property each object in the array contains. As far as I understand it as of Swift 3 sorted() does not sort in place, but instead returns a sorted array value. If you want to sort in place you should use sort(). But when I try the compiler keeps saying "sort() has been renamed sorted(by:) Why won't the compiler let me use sort() Here is my code: func sortAllArraysInDict() { for

Accessing values from Children Firebase

烈酒焚心 提交于 2019-12-12 03:43:07
问题 I am really not quite understanding the updates to the Swift 3.0 Firebase syntax, but most of all retrieving values from children of a snapshot seems to be impossible. To do so, I use a snippet like this: if let snapVal = snapshot.value as? [String: AnyObject] { for c in snapshot.children { let cx = (c as! AnyObject) let name = cx["NAME"] as! String } } I have taken many approaches to this but FIRDatabaseSnapshot has many restrictions in the new Swift 3 update, and AnyObject does not allow

animation not working in xcode 8 playground swift 3

北慕城南 提交于 2019-12-12 03:39:53
问题 I am trying to simply learn more about animations in Xcode 8 playground using swift 3. I have already learned that swift no longer uses XCPlayground, instead, using "import PlaygroundSupport". I can not get anything to show in the Assistant Editor. Does anyone know the fix for this or is it just a bug with the new Xcode 8 Any help would be awesome. Thanks. import UIKit import PlaygroundSupport let containerView = UIView(frame: CGRect(x: 0.0, y: 0.0, width: 375.0, height: 667.0))

Swift 3 How to manage different delegates for different Targets

丶灬走出姿态 提交于 2019-12-12 03:39:36
问题 We have created two Targets (Target_One & Target_Two) for same project. Target_One contain SDK1 and delegate Target1SDKHelperDelegate Target_Two contain SDK2 and delegate Target2SDKHelperDelegate The reason to create two target : we need to upload two apps with same UI but different SDK integration. As we know, each SDK has their own delegates. So we want to apply delegates specific to the target. Example: Target_One has a class named MyClass class MyClass: NSObject, Target1SDKHelperDelegate

UIPickerView cannot fetch Array output in Swift 3.0

怎甘沉沦 提交于 2019-12-12 03:39:21
问题 I have these PHP file to fetch data from database based on "dept" condition. getDeptList.php <?php if (isset($_POST['dept'])){ $dept = $_POST['dept']; $sql = "SELECT * FROM users WHERE dept ='$dept'"; $result = mysqli_query($connect, $sql); if ($result && mysqli_num_rows($result) > 0){ while ($row = mysqli_fetch_array($result)) { $login_db = $row['login']; $real_namedb = $row['real_name']; $output[]= array('login' => $login_db, 'real_name' => $real_namedb); } echo json_encode($output); mysqli

NSExpression\NSNumber extension changed in Swift 3.0

我与影子孤独终老i 提交于 2019-12-12 03:34:23
问题 have some extension for NSNumber: extension NSNumber { func factorialF() -> NSNumber { return tgamma(self.doubleValue + 1) } } then i use this extension in my calculation var stringFunction: String = "FUNCTION(10,'factorialF')" var expn: NSExpression = NSExpression(format: stringFunction) var result = expn.expressionValueWithObject(with: nil, context: nil) in Xcode 7.3.1 and Swift 2.2 all works but in Xcode 8 and Swift 3.0 i have some error in my extension NSNumber "No 'tgamma' candidates