9 Notes for me in February Swift 4.2


1. Error in debug area "this class is not key value coding-compliant for the key view."

-Your view controller may have the wrong class in your xib, maybe have a double connection (@IBOutet) xib to swift.

- It is caused by the Second view controller in main.xib having a class of UIViewController instead of SecondView. Changing to the correct class resolves the problem

2. simple create new repo GIT



3.  Found Error in code"scrollViewDidScroll" method does not override any method from its superclass swift 4


How do I solve this error? , implementing a method from the protocol, yes, but it's not an override.  An override is when your superclass also implements that method and you're providing a version that replaces or modifies the behavior of the superclass implementation. That's not what's happening here. So  Just remove the override keyword.

Before:


override scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard let height = navigationController?.navigationBar.frame.height else { return }
        moveAndResizeImage(for: height)
    }

After:

func scrollViewDidScroll(_ scrollView: UIScrollView) {
        guard let height = navigationController?.navigationBar.frame.height else { return }
        moveAndResizeImage(for: height)
    }



4. StackView Custom spacing items

Update For iOS 11, StackViews with Custom Spacing
Apple has added the ability to set custom spacing in iOS 11. You simply have to specify the spacing after each arranged subview. Unfortunately,  you can't specify spacing before.
stackView.setCustomSpacing(10.0, after: firstLabel)
stackView.setCustomSpacing(10.0, after: secondLabel)
Still way better than using your own views.

5. Save uiview as image to local data

change uiview to image use UIGraphicsBeginImageContextWithOptions and  drawHierarchy

UIGraphicsBeginImageContextWithOptions(CGSize(width: 375, height: 446), false, 0)
        subview.drawHierarchy(in: CGRect(x: 0, y: 0, width: view.bounds.size.width, height: viewHome.bounds.size.height), afterScreenUpdates: true)
        
  guard let imege: UIImage = UIGraphicsGetImageFromCurrentImageContext() else { return }

6. Push Notification firebase  didn't run if kill an app 

if I kill app or force close app I don't get notifications from firebase,  but if app running in the background I get a notif. but if straight away from firebase  app get notifications

try to use this code


func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        localNotification(object: userInfo)
        completionHandler(UIBackgroundFetchResult.newData)
    }


7. Action Underline in UILabel

To have the same attributes for all texts of one UILabel, I suggest you to subclass UILabel and overriding text, like that:

@IBAction func labelAction(_ sender: Any) {
        let underlineAttribute = [NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue]
        let underlineAttributedString = NSAttributedString(string: "This label use underline", attributes: underlineAttribute)
        label.attributedText = underlineAttributedString
    }

Or use class
class UnderlinedLabel: UILabel {
    override var text: String? {
        didSet {
            guard let text = text else { return }
            let textRange = NSMakeRange(0, text.count)
            let attributedText = NSMutableAttributedString(string: text)
            attributedText.addAttribute(NSAttributedString.Key.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: textRange)
            // Add other attributes if needed
            self.attributedText = attributedText
        }
    }

}

8. Remove Array with "Set Array"
https://www.hackingwithswift.com/sixty/2/2/sets

example:

let colors = Set(["red", "green", "blue"])
let colors2 = Set(["red", "green", "blue", "red", "blue"]) // ramove if array duplicate
print(colors, colors2)


9. Can check permission again?

The OS will only ever prompt the user once. If they deny permission, that's it. What you can do is direct the user to the Settings for your app by passing UIApplicationOpenSettingsURLString to UIApplication's OpenURL: method. From there, they can re-enable location services if they wish. That said, you probably shouldn't be too aggressive about bugging them for permission.

https://stackoverflow.com/a/29980902/8366535

use alertAction goto setting

let alertController = UIAlertController (title: "Title", message: "Go to Settings?", preferredStyle: .alert)
     
        let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
            guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
                return
            }
         
            if UIApplication.shared.canOpenURL(settingsUrl) {
                UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                    print("Settings opened: \(success)") // Prints true
                })
            }
        }
        alertController.addAction(settingsAction)
        let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
        alertController.addAction(cancelAction)
     
        present(alertController, animated: true, completion: nil)


https://stackoverflow.com/a/49917223/8366535




No comments: