swift2

Swift 2 JSON POST request [dictionary vs. String for HTTPBody of NSMutableURLRequest]

≡放荡痞女 提交于 2019-12-20 03:40:39
问题 I have the following swift code that submits a POST request successfully. let request = NSMutableURLRequest(URL: NSURL(string: url)!) let session = NSURLSession.sharedSession() request.HTTPMethod = "POST" request.HTTPBody = "foo=bar&baz=lee".dataUsingEncoding(NSUTF8StringEncoding) let task = session.dataTaskWithRequest(request, completionHandler: completionHandler) Instead of using query parameter like syntax, I'd like to use a dictionary, but when I do the following: let request =

UIView dynamic height depending on Label Height

六月ゝ 毕业季﹏ 提交于 2019-12-20 03:38:34
问题 I have a Label which takes dynamicaly some data from database. These data are strings which can sometimes be 3-4-5 rows etc. So this labe is inside a UIView. --UIView --Label How can i make the UIView to take the certain height of the Label dynamicaly?? 回答1: you can just do it with storyboard this pics set the label height relation to greater than or equal and set the view height relation to greater than or equal it work like a magic 回答2: Bellow is working solution of your problem. I used

Need help setting up an interface where rotation is fixed for most elements, but alert and sharing boxes auto-rotate with the device

≯℡__Kan透↙ 提交于 2019-12-20 03:31:38
问题 I'm working with Xcode 7 and Swift 2. I am working on an interface with a camera preview layer and controls that display in a manner similar to the native iOS camera app. The controls all stay in place as you turn the device, but the icons "pivot" in place to orient properly for the device orientation. (I hope I explained that in a way that makes sense. If not, open the native camera app on your iPhone and turn the device around a few times to see what I'm talking about.) I have the basic

Using 'self' on RxSwift closures… What about instance methods as param?

白昼怎懂夜的黑 提交于 2019-12-20 03:24:15
问题 In other stack overflow questions, it was emphasized that the capture [weak self] should be used for closures that aren't owned by the class because self could be nil before the closure completes. An alternative when the closure is owned by the class itself is [unowned self] . My question is do I need to use [unowned self] when the function I pass as a parameter is an instance method of the current class? Example import RxSwift class Person { var name = "Default name" class func getPersons()

See characters in an NSCharacterSet (Swift) [duplicate]

早过忘川 提交于 2019-12-20 02:46:30
问题 This question already has answers here : NSArray from NSCharacterSet (9 answers) Closed 3 years ago . When I'm trying to list characters in an NSCharacterSet like this print(NSCharacterSet.URLQueryAllowedCharacterSet()) it doesn't print out the characters but rather something similar to <__NSCFCharacterSet: 0x1759b900> 回答1: Based on another answer, here is a derived cleaner version in Swift 2.0/3.0: extension NSCharacterSet { var characters:[String] { var chars = [String]() for plane:UInt8 in

Swift optionals - warning on conditional cast from 'x' to 'x' always succeeds

回眸只為那壹抹淺笑 提交于 2019-12-20 02:45:19
问题 I was wondering if there is a way to turn off/avoid 'yellow' warnings in xcode on if let...NSUserDefaults constructs where the key is of a known value. For example: if let x = NSUserDefaults.standardUserDefaults().integerForKey("myKey") as? Int {...} Because of the if let I have to use as? . However, as I am using a known value type (in this case integer) the as? Int is effectively redundant - which is why I am getting the 'yellow warning'. Thoughts? Is there a better way to code these types

Binary Operator + Cannot be Applied to Operands of Type CGfloat int

别等时光非礼了梦想. 提交于 2019-12-20 02:32:56
问题 I am having the same problem as earlier with a different line of code; but this time, I wasn't able to fix it with the same approach as last time: var Y : Int = 0 var X : Int = 0 @IBOutlet var ball : UIImageView! ball.center = CGPointMake(ball.center.x + X, ball.center.y + Y) This is the error I am getting: binary operator + cannot be applied to operands of type CGfloat int 回答1: Declare them, instead, as the following: let X : CGFloat = 0.0 let Y : CGFloat = 0.0 Replying to your comment: The

Dynamic datasource/delegates for UITableView in swift

Deadly 提交于 2019-12-20 01:42:16
问题 I need to set up different objects based on certain conditions as the datasource & delegate for table view. But I am not able to assign tableview's datasource/delegate as it throws some errors. Cannot assign a value of type NSObject? to a value of type UITableViewDelegate? I did check this Q&A but this did not work. var dataSourceDelegate:NSObject? class RootViewController: UIViewController { ... override func viewDidLoad() { dataSourceDelegate = TableDataSourceDelegate() // Table View

wkwebview auto fill login form swift 2

痴心易碎 提交于 2019-12-20 01:10:30
问题 im new in the Swift World. I have a big problem ... I've tried to write an app that has only one WKWebView. It is like a web browser. The app should be able to fill die LogIn Fields automatically from an object or a list. But for test reasons i have put the login data into variables. class ViewController: UIViewController , WKNavigationDelegate{ // Zugänge let pw : String = "test" let bn : String = "test@test.com" // Adresse let adresse : String = "https://www.facebook.com" var webView:

Using inout keyword: is the parameter passed-by-reference or by copy-in copy-out (/call by value result)

牧云@^-^@ 提交于 2019-12-20 01:05:39
问题 Question: Based on the information and discussion below: Are inout parameters passed-by-reference or by copy-in copy-out ? Based on the following SO threads, function parameters marked by the inout keyword is passed by reference : Does inout/var parameter make any difference with reference type? Is Swift Pass By Value or Pass By Reference Why doesn't inout pass by reference? We note that the two top-most threads are pre-Swift 2.0; I haven't been able to find any newer discussion on the