Tag: swift

When should I compare an optional value to nil?

Quite often, you need to write code such as the following: if someOptional != nil { // do something with the unwrapped someOptional e.g. someFunction(someOptional!) } This seems a bit verbose, and also I hear that using the ! force unwrap operator can be unsafe and best avoided. Is there a better way to handle […]

Finding angle b/w two points

Here is my problem, whenever I tap on any button it should come to center, I am using CGAffineTransform and CABasicAnimation, but I am not able to get the angle I should rotate with, each of them is a button in circle, any help will be appreciated code for rotation func rotateBarrel(with duration:CGFloat,angle:Double) { let […]

UIImageView changes their position when the UITableViewCell is selected

I have an UIImageView inside of a UITableViewCell, and when I select the cell the imageView changes their position like this: Before tapping: After tapping: Is this a bug with iOS or something I’ve done wrong? Edit: didSelectRowAt code: func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { let list = indexPath.section == 0 ? subscribed****s […]

tapGestureRecognizer.location brings nil in UICollectionView

In my swift app I created class that delegates UICollectionViewController. Additionally, I have other class responsible for handling ‘UICollectionReusableView`. So in the first class I have a method: override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: “Header”, for: indexPath) as! UserProfileHeaderCollectionReusableView and thanks […]

Popover position is not correctly after its orientation change in swift

I have a popover in my view . after rotate my app position of it is not correct and it shown out of my cadre.I x and y of popover in orientation observer. can you hep me? @objc func onOrientationChange() { if let firstItem = indexPaths.first, let cell = (viewController as? LettersViewController)?.tableView.cellForRow(at: firstItem), indexPaths.count == […]

Upload image with parameters in Swift

I’m trying to upload an image with parameters in Swift. When I try this code, I can get the parameters but not the image uploadFileToUrl(fotiño:UIImage){ var foto = UIImage(data: UIImageJPEGRepresentation(fotiño, 0.2)) var request = NSMutableURLRequest(URL:NSURL(string: “URL”)) request.HTTPMethod = “POST” var bodyData = “id_user=”PARAMETERS&ETC”” request.HTTPBody = bodyData.dataUsingEncoding(NSUTF8StringEncoding); request.HTTPBody = NSData.dataWithData(UIImagePNGRepresentation(foto)) println(“miraqui \(request.debugDescription)”) var response: AutoreleasingUnsafeMutablePointer<NSURLResponse?>=nil var […]

How can I use NSTimer in Swift?

I tried var timer = NSTimer() timer(timeInterval: 0.01, target: self, selector: update, userInfo: nil, repeats: false) But, I got an error saying ‘(timeInterval: $T1, target: ViewController, selector: () -> (), userInfo: NilType, repeats: Bool) -> $T6’ is not identical to ‘NSTimer’