#Coding

UIButton addTarget does not work inside custom UITableViewCell

by

When it comes to programmatically creating custom UITableViewCells, there's a common stumbling block that developers often encounter. It's the scenario where you add a button to the cell, but the target action doesn't get triggered when the button is pressed. While there could be several factors contributing to this issue, I'll delve into one of the fundamental design flaws that can lead to such behavior.

The crux of the problem often lies in the way we add subviews to our custom cell programmatically. A common practice is to add the subviews directly to the cell itself. However, this approach can cause unexpected behavior, including the failure of target action calls when a button is pressed.

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        addSubview(button)
        button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
}

So, what’s the solution? Let’s dive into it.

CONTINUE READING

How to convert Swift String to Array of String

by

To convert the string literal into an array of strings, we use the map function on the string literal.

map(_:)

Returns an array containing the results of mapping the given closure over the sequence’s elements.

CONTINUE READING

How To Efficiently Read Set Lines of Range in Text File

by

In order to crawl a large text file containing URLs, I required a method that would allow me to specify and read only a certain range of lines, without having to load the entire file into memory. To achieve this, I utilized the islice() function from Python's itertools module: itertools.islice()

Here is a code snippet that demonstrates this:

CONTINUE READING

How to remove more than one item at once from Array with matching condition in Swift

by

In Swift most sequence collections provides removeAll(where:) - an efficient way to remove at once multiple items matching the coditional logic from collection.

removeAll(where:)

Removes all the elements that satisfy the given predicate.

CONTINUE READING

How to limit the number of characters in a UITextField or UITextView

by

Very common user experience we come across when using UITextField and UITextview is to limit the number of characters.

It’s implementation is straightforward using their standard delegates implementing either shouldChangeCharactersIn (for text fields) or shouldChangeTextIn (for text views).


For UITextField:


func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if (textField.text?.count ?? 0) +  (string.count - range.length) >= characterLimit{
        return false
    }
    
    return true
}


For UITextView:

CONTINUE READING

How to sort an array of objects by a property value in JavaScript

by

There is always a need to sort an array. What if array consists of custom objects? How do we sort array of custom objects by a property value? Let us find out!


const array = [
    { name: ‘John’, order: ‘2’ },
    { name: ‘Jack’, order: ‘1’ },
    { name: ‘James’, order: ‘10’ }
]

CONTINUE READING

Error 400: Remote Push Notifications Not Working For iOS

by

In my localhost server, remote push notifications worked fine. It had stopped working on the production server. I have designed a neat system to separate the development environment from production mode for testing remote push notifications. Different certificates are used automatically based on the running environment. Device tokens are stored in a database based on the nature of the iOS app’s distribution. It ensures sandbox mode uses debug-version of device tokens else we will be running often into the “Baddevice Token” response from the apns server.

The system is well-tested with time. Remote push notifications stopped working in production mode all of sudden. I am not using the third-party service to manage our push notifications. I have our well-designed and well-tested middleware to manage push notifications. It follows a modern recommendation from apple to use http/2 to process all requests to apns server. Many good tutorials out there on the subject. I will share my troubleshooting experience and its solutions.

I turned on the verbose settings to display every information of cURL execution. It gives insight on if the connection is going through the apns server and what response we get.


CURLOPT_VERBOSE => true
CONTINUE READING

Artisan Error: Failed to listen on localhost:8000

by

I was creating an admin dashboard based on Laravel. UI components were reactive and build upon the vuejs framework. I will have a post on my experience with the vuejs framework someday.

I was switching between a compilation of Vue component changes and running the laravel localhost server. A regular serve command to start localhost:


php artisan serve —host 192.168.10.203 —port 8000

I was serving at the IP address of the machine so I could test conveniently from other machines and devices connected to LAN.

Between development precess somehow the laravel artisan process mustn’t have closed properly. When I attempted to serve localhost I received this error:


Failed to listen on localhost:8000(reason: Address already in use)

CONTINUE READING

How to Make Struct Hashable in Swift 5

by

We have used struct for purposes like small-scale copiable entity representing common-kinds-of-data. What if we want to make our custom struct model equatable? We want our struct to be analysed for basic equality of comparison. What if we have an array of struct and we want to perform some operation to array like sorting or create an unique array by removing duplicate structs.

We have seen this error shown when we try to perform such operation of comparison:



Type ‘CustomStruct’ does not conform to protocol ‘Hashable’

According to Apple’s documentation:

You can use any type that conforms to the Hashable protocol in a set or as a dictionary key. To use your own custom type in a set or as the key type of a dictionary, add Hashable conformance to your type. The Hashable protocol inherits from the Equatable protocol, so you must also satisfy that protocol’s requirements.

CONTINUE READING

No Matching Distribution Found for Botocore: AWSEBCLI

by

I have been ignoring the message to upgrade awsebcli tool for long time. This time I did not ignore and made an attempt to upgrade:


pip install --upgrade awsebcli
I got an error:


No matching distribution found for botocore<1.22.0,>=1.21.0 (from awsebcli)

There was also a little warning asking me to consider upgrading my existing pip installation. I upgraded pip with simple command.

pip install --upgrade pip --user
I made fresh attempt to upgrade awsebcli. I got same error telling me “No matching distribution found” I found a suggestion in forum to try installing specific verion of awsebcli to overwrite current installation version. I did that:

sudo pip install --upgrade awsebcli botocore==1.19.63
I force upgraded to 1.19.63 which was still way higher than my existing version of awsebcli. It did installed successfully but also gave few errors:


ERROR: pip's legacy dependency resolver does not consider dependency conflicts when selecting packages. This behaviour is the source of the following dependency conflicts.
boto3 1.7.68 requires botocore<1.11.0,>=1.10.68, but you'll have botocore 1.19.63 which is incompatible.
awsebcli 3.20.2 requires botocore<1.22.0,>=1.21.0, but you'll have botocore 1.19.63 which is incompatible.

You might also like: HTTPS not working on AWS Elastic Beanstalk

It installed with errors. I verified installation by deploying my new update to beanstalk environment to see if awsebcli tool is working fine. It did not. I got this error:



File “/usr/local/bin/eb”, line 5, in 
    from ebcli.core.ebcore import main
  File “/Library/Python/2.7/site-packages/ebcli/core/ebcore.py”, line 19, in 
    from ebcli.core import ebglobals, base, hooks
  File “/Library/Python/2.7/site-packages/ebcli/core/hooks.py”, line 20, in 
    from ebcli.core import fileoperations
  File “/Library/Python/2.7/site-packages/ebcli/core/fileoperations.py”, line 32, in 
    from json import load, JSONDecodeError
ImportError: cannot import name JSONDecodeError

Solution:

CONTINUE READING

Recommended posts