#Blog

Blog articles on programming and problems focused on iOS and some web. I also write stories on my travel and hiking pursuits outside my work.

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


How to resize an Image of UIButton in Swift?

by

Sometime there may be need to resize the image displayed in UIButton for not availability of proper sized image because of some reason. Wrong size gives unwanted results like stretched out image pixels etc. In this post we are assuming simple UIButton with image only.

First solution:

We can update contentMode settings of imageView property of UIButton. Default value is .scaleToFill. It scales the image to fill the UIButton ignoring aspect ratio.


btn.imageView?.contentMode = .scaleAspectFit

This will solve the 50% of problem because UIButton will render the image maintaining the aspect ratio.

Now if in worst case scenario problem isn’t solved because of bigger dimension of image then we have second solution:

CONTINUE READING


How To Scroll UICollectionView To End Of Footer or Header

by

Recently someone brought me a problem that needed my perspective. A vertical UICollectionView is using sections for displaying items. Requirement is to scroll to bottom of UICollectionView. A method that can be used used for scrolling to last item in UICollectionView is

scrollToItem(at indexPath: IndexPath, at scrollPosition: UICollectionView.ScrollPosition, animated: Bool)
Scrolls the collection view contents until the specified item is visible.

This method worked incomplete. It made collection view to last item. Isn’t that what is required? There is a custom footer at the end of section and footer needs to be visible. It did not scroll to last footer but stopped just before footer.

To find the information regarding offset on our footer view we will gather layout information for that particular supplementary view. Remember supplementary view can be either header or footer so this works for both. Here is an extension to UICollectionView

CONTINUE READING


A guide to Annapurna Base Camp: Frequently asked Questions

by

Frequently asked questions on Annapurna Base Camp trek.

  • Where can I start ABC trek to save time?
  • How to reach Ghandruk directly from Pokhara?
  • What is the shortest ABC trek route?
  • How difficult is the ABC trek?
  • How are the conditions of Tea Houses?
  • What are the costs of food and shelter?
  • Can I get altitude sickness at ABC?
  • Is Jhinu Hotspring worth visiting?
  • Do I need Rain Cover?

CONTINUE READING


iOS UIKit Animation Equivalent in Android's Kotlin Animation

by

Animation is a common feature we use often in our apps. iOS SDK does provide different ways to implement. From higher-level UIKit methods to more lower Core Animation. Any UIKit based animation is internally translated into core animations. UIKit methods abstracts the layer of core animation.

Today here we will see the higher-level UIKit animation’s equivalent in Android’s Kotlin - one of the ways that Android SDK provides us. We will use ViewPropertyAnimator - a simplified and optimised way to animate properties of a View in Android. A simple example to rotate a view by certain angle for a duration and also callback implementation when animation ends.

Below is the equivalent code snippet for Android Animation in Kotlin and iOS animation on views with UIKit.

CONTINUE READING


How to Present a View Modally From UITabBarController in Swift

by

Each tab of a tab bar controller is associated with a view controller. When the user selects a specific tab, the tab bar controller displays the root view of the corresponding view controller, replacing any previous views. The tab bar interface displays tabs at the bottom for selecting between the different display views.

Tab bar controller lets us to implement easy multi-selection of displays easily. In this user interface style tabs at the bottom remains visible. Display switches between selection at child controller section above.

Comes a situation where view controller associated with a specific tab to display modally or push where Tabs at bottom gets covered behind this view controller. Instagram app is a good example where when create-post-tab is pressed, then display is opened over the tab controller.

How to present UITabBarItem’s view controller modally?

CONTINUE READING


How to Disconnect CoreBluetooth Peripheral cleanly in Swift

by

To disconnect a connected peripheral in CoreBluetooth we call method cancelPeripheralConnection


centralManager.cancelPeripheralConnection(peripheral)

This did disconnect peripheral in CoreBluetooth. In my program there is code that caches old discovered peripherals for certain period. It does go through cached peripherals to search for their services. What I found was that even if peripheral was disconnected code was somehow discovering the services and characteristics. There is a segment of code which is written to trigger certain behaviour when characteristics of peripheral is discovered. I did not want this for disconnected peripherals. Problem is not an issue for there are very simple solution for this scenario like checking the state of peripherals before trigger and etc.

I am emphasizing that Disconnected peripheral’s services still can be discovered in CoreBluetooth. Which is not an issue. I had assumed that when peripheral gets disconnected it removes its services and characteristics.

These methods do get called up even if peripheral is no more connected.


func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)
func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?)
CONTINUE READING


How to detect click inside StaticBody2D in Godot Game Engine?

by

A simple approach to detect mouse click in our game is to intercept input event in in-built _input method. Let’s intercept the input.


extends StaticBody2D
func _input(event):
    if event is InputEventMouseButton:
        if event.button_index == BUTTON_LEFT and event.pressed:
            print(“A click!”)

The problem is that input detection is registered all over the viewport. Let me explain. I have a Sprite child node inside StaticBody2D node. My intention is to detect mouse input over the visible sprite only. In our 2D game we may want to select an object in our world with mouse and do some interaction with it. In this case mouse input is been registered outside the Sprite node.Note that our script is attached to StaticBody2D. As a result this node has to have a shape, so the example has a collision shape as a child. We are trying to keep Sprite node loosely coupled from the logic. Documentation clearly says that StaticBody2D is not intended to move. This makes perfect for objects like trees in the game.

Let’s come back to our problem. My assumption for the behaviour would be that StaticBody2D is shapeless. Yes, we did add a collision shape so it mouse input should be triggered only over defined collision shape. However, StaticBody2D is behaving shapeless and mouse input is registered all over the screen.


A simple approach I discovered is two steps.

CONTINUE READING


How to Set and Restrict the Camera 2d Boundary in Godot Game engine

by

I have been learning and practicing Godot game engine by watching free high quality video series from HeartBeast. I really want you to go and watch this RPG tutorial series to learn practical fundamentals of Godot engine. In part-20 of the series we learned about setting up camera and make camera follow the player around the level. It is very easy in Godot.

Above tutorial video lets you learn to easily setup player Camera2D. One thing was missing and I took a challenge exercise where camera stops following the player when player is near the edge or boundary of the level. Currently camera moves with player and vast empty space is visible when player is near boundary of the level.

My solution is based on existing type of game world I am working with.

CONTINUE READING


Recommended posts