9 Notes November 2021

1. Cannot assign to property: 'parameters' is get-only property



to fixing that error add "set" after  {get}

//Before
var parameters: [String: Any]? { get }
//After var parameters: [String: Any]? { get set }


2.  When keyboard appear textfield scroll up

when the keyboard covers the display of the textfield when it appears,  here are two ways you can do

#First method
If textfield is in UIView, you can use this method

//MARK: - Function keyboard notif

    private func subscribeNotification() {
        NotificationCenter.default.addObserver(self, selector: #selector(didShowKeyboard(_:)), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(didHideKeyboard(_:)), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    // MARK: - Keyboard
    
    @objc
    func didShowKeyboard(_ notification: NSNotification) {
        if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
            if self.frame.origin.y == 0 {
                self.frame.origin.y -= keyboardSize.height
            }
        }
    }
    
    @objc
    func didHideKeyboard(_ notification: NSNotification) {
        if self.frame.origin.y != 0 {
            self.frame.origin.y = 0
        }
    }

#second method

If you use textfield into tableView you can use this, it scroll textField in tableViewCell

     private func subscribeNotification() {
        NotificationCenter.default.addObserver(self, selector: #selector(didShowKeyboard), name: UIResponder.keyboardWillShowNotification, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(didHideKeyboard), name: UIResponder.keyboardWillHideNotification, object: nil)
    }
    
    // MARK: - Keyboard
    
    @objc func didShowKeyboard(notification: Notification) {
        if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {
            print("Notification: Keyboard will show")
            let edgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)
            tableView.contentInset = edgeInsets
            tableView.scrollIndicatorInsets = edgeInsets
        }
    }
    
    @objc func didHideKeyboard(notification: Notification) {
        print("Notification: Keyboard will hide")
        UIView.animate(withDuration: 0.2, animations: {
            self.tableView.contentInset = UIEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)
        })
    }


3. Failed install "zsh: command not found: fastlane"

In condition you want to run fastline command like "$fastlane match development" but you get this info  "
zsh: command not found: fastlane"

solution is follw this step

$xcode-select --install
$
sudo gem install fastlane

after install complete try to install this again

$fastlane match development

success like this image

image: fastlane success




4. Error code "awakeFromNib"

when you use func like this in the UIView file if you call data into this func 

author image

you can get this error " cannot assing of type ...

author image

to fixing that you can remove class from that function like this 


author image




5. Error run fastlane match

[11:29:30]: There are no local code signing identities found.

You can run `security find-identity -v -p codesigning` to get this output.

This Stack Overflow thread has more information: https://stackoverflow.com/q/35390072/774.

(Check in Keychain Access for an expired WWDR certificate: https://stackoverflow.com/a/35409835/774 has more info.)

[11:29:31]: Enter the password for /Users/user/Library/Keychains/login.keychain-db

[11:29:31]: This passphrase will be stored in your local keychain with the name fastlane_keychain_login and used in future runs

[11:29:31]: This prompt can be avoided by specifying the 'keychain_password' option or 'MATCH_KEYCHAIN_PASSWORD' environment variable

[11:29:31]: Password for login keychain: ********

[11:32:51]: Type password for login keychain again: ********

[✔] Setting key partition list... (this can take a minute if there are a lot of keys installed) 

[11:32:57]: Using keychain password from keychain item fastlane_keychain_login in /Users/Library/Keychains/login.keychain-db

[✔] Setting key partition list... (this can take a minute if there are a lot of keys installed) 


Solution: double click your certificate (.cer)




6. Error running app camera and library

when run app in simulator xcode 13 get photo library and I get this error.
2021-10-21 08:04:26.209491+0700 [61641:5526204] [db] _LSSchemaConfigureForStore failed with error Error Domain=NSOSStatusErrorDomain Code=-10817 "(null)" UserInfo={_LSFunction=_LSSchemaConfigureForStore, ExpectedSimulatorHash={length = 32, bytes = 0x5ea962b6 2743d2a5 6592cba5 72884713 ... ba2bfb1a dece9a05 }, _LSLine=409, WrongSimulatorHash={length = 32, bytes = 0x0aee9f40 851e8e32 0c41b663 75d10195 ... 5a6ec0a3 8d2bc387 }}
2021-10-21 08:04:26.209773+0700 [61641:5526204] [db] Failed to initialize client context with error Error Domain=NSOSStatusErrorDomain Code=-10817 "(null)" UserInfo={_LSFunction=_LSSchemaConfigureForStore, ExpectedSimulatorHash={length = 32, bytes = 0x5ea962b6 2743d2a5 6592cba5 72884713 ... ba2bfb1a dece9a05 }, _LSLine=409, WrongSimulatorHash={length = 32, bytes = 0x0aee9f40 851e8e32 0c41b663 75d10195 ... 5a6ec0a3 8d2bc387 }}
2021-10-21 08:04:30.101868+0700 [61641:5527323] [db] _LSSchemaConfigureForStore failed with error Error Domain=NSOSStatusErrorDomain Code=-10817 "(null)" UserInfo={_LSFunction=_LSSchemaConfigureForStore, ExpectedSimulatorHash={length = 32, bytes = 0x5ea962b6 2743d2a5 6592cba5 72884713 ... ba2bfb1a dece9a05 }, _LSLine=409, WrongSimulatorHash={length = 32, bytes = 0x0aee9f40 851e8e32 0c41b663 75d10195 ... 5a6ec0a3 8d2bc387 }}
2021-10-21 08:04:30.101980+0700 [61641:5527323] [db] Failed to initialize client context with error Error Domain=NSOSStatusErrorDomain Code=-10817 "(null)" UserInfo={_LSFunction=_LSSchemaConfigureForStore, ExpectedSimulatorHash={length = 32, bytes = 0x5ea962b6 2743d2a5 6592cba5 72884713 ... ba2bfb1a dece9a05 }, _LSLine=409, WrongSimulatorHash={length = 32, bytes = 0x0aee9f40 851e8e32 0c41b663 75d10195 ... 5a6ec0a3 8d2bc387 }}
2021-10-21 08:04:30.102513+0700 [61641:5527323] [xpc.exceptions] <NSXPCConnection: 0x6000009ecdc0> connection on anonymousListener or serviceListener from pid 61658: Warning: Exception caught during invocation of selector didSelectMediaWithInfoDictionary:, dropping incoming message and invalidating the connection. Exception: *** -[NSURL URLByAppendingPathExtension:]: component, components, or pathExtension cannot be nil. *** -[NSURL URLByAppendingPathExtension:]: component, components, or pathExtension cannot be nil. ( 0 CoreFoundation 0x000000011bef0fba __exceptionPreprocess + 242 1 libobjc.A.dylib 0x000000011af61ff5 objc_exception_throw + 48 2 Foundation 0x000000010daeb39e -[NSURL(NSURLPathUtilities) URLByDeletingPathExtension] + 0 3 PhotosUI 0x000000014020295f -[PUPhotoPickerExtensionHostContext _UIImagePickerControllerInfoDictionaryFromPhotoPickerInfoDictionary:] + 2203 4 PhotosUI 0x0000000140201f36 -[PUPhotoPickerExtensionHostContext didSelectMediaWithInfoDictionary:] + 34 5 Foundation 0x000000010dbb3bd7 __NSXPCCONNECTION_IS_CALLING_OUT_TO_EXPORTED_OBJECT_S1__ + 10 6 Foundation 0x000000010dbb290f -[NSXPCConnection _decodeAndInvokeMessageWithEvent:flags:] + 2268 7 Foundation 0x000000010dbb3f06 message_handler + 206 8 libxpc.dylib 0x000000011f213cf8 _xpc_connection_call_event_handler + 56 9 libxpc.dylib 0x000000011f21407c _xpc_connection_mach_event + 891 10 libdispatch.dylib 0x000000011ee377ee _dispatch_client_callout4 + 9 11 libdispatch.dylib 0x000000011ee5180a _dispatch_mach_msg_invoke + 550 12 libdispatch.dylib 0x000000011ee3ddb1 _dispatch_lane_serial_drain + 307 13 libdispatch.dylib 0x000000011ee526c8 _dispatch_mach_invoke + 555 14 libdispatch.dylib 0x000000011ee3ddb1 _dispatch_lane_serial_drain + 307 15 libdispatch.dylib 0x000000011ee3ec9d _dispatch_lane_invoke + 490 16 libdispatch.dylib 0x000000011ee4aa7a _dispatch_workloop_worker_thread + 872 17 libsystem_pthread.dylib 0x000000011f30a45d _pthread_wqthread + 314 18 libsystem_pthread.dylib 0x000000011f30942f start_wqthread + 15

My solution: I dont know how to fix it I'll just try to run with device and it will be okey.


7. Invalid type in JSON write 

I'd get some crash when build wrong json post. this error response
------------------------------------------------------------------------------

ibc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (__SwiftValue)'
terminating with uncaught exception of type NSException
CoreSimulator 776.3 - Device: iPhone 12 (0BFF2E0C-E5F4-4AED-B3B7-920D8EAF2400) - Runtime: iOS 14.5 (18E182) - DeviceType: iPhone 12
_LSContextInitReturningError() failed with error Error Domain=NSOSStatusErrorDomain Code=-10817 "(null)" UserInfo={_LSFunction=_LSSchemaConfigureForStore, ExpectedSimulatorHash={length = 32, bytes = 0x5ea962b6 2746d2a5 6592cba5 72884713 ... ba1bfb1a dece9a05 }, _LSLine=409, WrongSimulatorHash={length = 32, bytes = 0x0aee9f40 851e8e32 0c41b663 75d10195 ... 5a6ec0a3 8d2bc387 }}
(lldb) 
------------------------------------------------------------------------------


8.  Set background color follow the UILabel character

When I need to set background follow the uilabel this I found in stackoverflow can help mu code
and my code look like this

label.text = "  MORE LIVES  "
guard let value = label.text else {return}
let attributeString = NSMutableAttributedString(string: value)
var startIndex = 0
attributeString.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.red, range: NSRange(location: startIndex, length: value.count))
startIndex = startIndex + value.count + 1
label.attributedText = attributeString
       

this my example from stackoverflow, thanks for answer
   label.text = """
            GET
            MORE LIVES
            """
           label.numberOfLines = 0
let attributeString = NSMutableAttributedString(string: label.text!)
let labelText = label.text!
var lines: [String] = [] labelText.enumerateLines { line, _ in lines.append(line) } print(lines)//lines in your text var startIndex = 0 for value in lines { //Apply background color to lines attributeString.addAttribute(NSAttributedString.Key.backgroundColor, value: UIColor.red, range: NSRange(location: startIndex, length: value.count)) startIndex = startIndex + value.count + 1 //startIndex will locate new line's first index } //Assign attributedText to your label label.attributedText = attributeString



9.  Marging array dictionary to single dictionary

I found error post api when I get data array [[String: Any]] and I want to change to single dictionary
like this [String: Any], and this solve my problem :)

this example use model to single [String: Any] for post API parameters

struct ParamFormPersonal {
    var key: String
    var label: String
    var value: String
    
    func parameters() -> [String: Any] {
        return [
            key: value
        ]
    }
}

var dataArray = [ParamFormPersonal]()

// append with model
dataArray.append(ParamFormPersonal(key: "key1", label: "hahah", value: "one")) dataArray.append(ParamFormPersonal(key: "key2", label: "hahah", value: "two")) dataArray.append(ParamFormPersonal(key: "key3", label: "hahah", value: "three")) // this how to marging array dictionary var dataIs = [String: Any]() dataArray.forEach { data in dataIs = dataIs.merging([data.key : data.value]) { (current, _) in current } } print(dataIs) //["key3": "three", "key2": "two", "key1": "one"]
see thin link can help you :)
source: https://stackoverflow.com/a/43615143/8366535

====================================================

Handle height tableview auto height into cell
https://21zerixpm.medium.com/tableview-in-uitableviewcell-3c7cf123d969

====================================================

No comments: