#objc

How to Get Substring With NSRange in Swift 5

by


Warning:

substring(with:)’ is deprecated: Please use String slicing subscript.

In Swift 5 we do no longer have access to substring() methods for they are deprecated. We will discuss all alternatives and understand them in details.

  • How to access Characters in String?
  • Why Int based Subscript is not available in Swift?
  • What is difference between Range and NSRange?
  • How does String Indices work?
  • How to create Range from String Indices?
  • How to get substring with Range?
  • How to get substring with NSRange?

CONTINUE READING

How to Fix Warning Attempt To Present UIAlertController in Swift

by

Warning: Attempt to present <UIAlertController: 0x7fa95080de00> on <MainViewController: 0x7fa94f8a5000> which is already presenting (null)

Warning of attempt to present view controller is common occurrence when making an easily made mistake. I will take Alert controller for an example to discuss.

Lets’s create a scenario to bring UIAlertController warning message and observe side effect to understand our situation more effectively. Then we will understand on how to fix warning attempt to present UIAlertController. I will present a Detail view controller modally from Main controller, and Main controller will be delegate object assigned to the property of Detail view controller. In our presented view controller I have a dismiss code when back button is pressed. Before dismissing the controller I will call the delegate object - Main controller in this case - to present Alert controller from Main controller.

CONTINUE READING

MFMessageComposeViewController shows blank white screen issue

by

We use an MFMessageComposeViewController object to display the standard message composition interface in order to send text messages in our app. Properties of MFMessageComposeViewController lets us populate pre-defined recipients and body message before presenting the interface.

Simple code to create and present MFMessageComposeViewController is:

            
if([MFMessageComposeViewController canSendText]){
    MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
    picker.messageComposeDelegate = self;
    picker.recipients =@[“1234”];
    picker.body = @“pre-defined message”;
    [self presentViewController:picker animated:YES completion:nil];
}
else{
    NSLog(@“Cannot send text”);
}
            
            
            
if MFMessageComposeViewController.canSendText() {
    let picker = MFMessageComposeViewController()
    picker.messageComposeDelegate = self;
    picker.recipients =[“1234”];
    picker.body = “pre-defined message”;
    self.present(picker, animated: true, completion: nil)
}
else{
    print(“Cannot send text”);
}
            
            

Not so distant, I had stumbled upon a very silly mistake that caused an MFMessageComposeViewController to show blank after presenting.

CONTINUE READING

inputAccessoryView ignores bottom Safe Area in iPhone X's

by

I have this neat view controller setup for my comments system. At the bottom of the screen is inputAccessoryView which will be fixed until interacted with to bring keypad up for user to post comments. When keypad is up view will be above it and if keypad is dismissed then view will stick at the bottom of screen.

To achieve this I override two methods of my UIViewController:


-(BOOL)canBecomeFirstResponder{
    return YES;
}

-(UIView *)inputAccessoryView{
    return commentInputView.contentView;
}

I am using custom xib and my custom input view is composed within the class.

It works fine except in iPhone with notch.

CONTINUE READING

Auto sizing UICollectionViewCell of UICollectionView fails in iOS 12

by

The content of my UICollectionView flows in horizontal direction. Height of items are fixed but width of each are dynamic based on the length of text. For this example I am using only UILabel in my UICollectionViewCell. My UICollectionViewCell is custom xib.

These are the constraints I have applied to UILabel:
  • Leading : 0
  • Trailing : 0
  • Top : 0
  • Height : 44

CONTINUE READING

Sort NSArray of custom objects with more nesting

by

Sorting an array of custom objects is fair simple. NSSortDescriptor works awesome for this. Lets see an example:


@interface Parent : NSObject
@property(strong, nonatomic) NSString *name;
@end
NSArray *arrayOfParents = @[parent1, parent2];

If I want to sort array of parents by their name then I would use something like:

CONTINUE READING

drawViewHierarchyInRect vs renderInContext

by

I came across very curious problem whose cause I couldn’t have come to know about it without serious debugging because this was something that didn’t catch my attention. I had a task that required capturing snapshot of UIView with hierarchy of subviews and perform few interactive transformation animations on view immediately after - giving feeling of instant multitask. Delay should have been negligent for naked eyes or tolerant but it wasn’t.

CONTINUE READING

Recommended posts