Monday, November 9, 2015

How to retrieve and select all labels in Swift

I've recently started doing Swift development, and was tasked with finding a way to select each label that was swiped over in an app. I made multiple attempts at this, first trying a gesture recognizer, and then trying to implement code in the touchesBegan and touchesMoved methods to no avail. I could select a single label, but couldn't get multiple without implementing a gesture recognizer for each label (and that would be extremely repetitive, extraneous code). After a ton of Googling, I finally asked for help in the Swift subreddit, which got me REALLY close. The only thing I was missing was how to get a collection of each label in the app.
My first thought was to create an outlet for each label, and then create an array containing a reference to each label and using that, but that would again be time consuming, and just didn't seem efficient to me. I asked the author of the Reddit response what he was using in his code as an allLabels() function, but didn't get a response there, so I kept Googling. After a couple more days, I finally came across this beautiful Stack Overflow. They were getting an array of buttons, but I assumed that I could apply the same code to labels and voila! Problem solved, and it's SO simple. The code I ended up with for getting an array of all labels is as follows:
private var labels:[UILabel] = [UILabel]()
    
    override func viewWillAppear(animated: Bool) {
        for v:AnyObject in self.view.subviews {
            if v is UILabel {
                labels.append(v as! UILabel)
            }
        }
    }
Hope this helps anyone doing a similar search!

Thursday, March 19, 2015

minFraud Ruby gem

So I'm working on this new project that connects to the minFraud service, and there isn't a whole lot out there Ruby-wise for working with the API. The one gem that I did find is called minfraud-ruby. However, this gem seemed very limited. The minFraud service itself only requires you to send it an IP address, and a license key, but the gem also required city, state, zip and country. When you're working with IP addresses from countries outside of the US, some or all of these attributes will not be available. The gem also only returned a risk score. While the risk score is useful, there's much more useful data returned in the response from minFraud, such as ip_city, ip_region (which in the US is the state), ip_country_name, etc.
After working with the limitations for a while, I decided to fork the gem and give it a bit of an overhaul. I updated the required fields, so that now all you need is a license key and an IP address to get a response. Providing additional information will get you a more accurate response from the service, but this is not required. I also exposed a number of additional attributes in the transaction object, so you can retrieve things like city, state and country without any additional steps.

Happy coding, kids. Until next time..