Get the length of a String

As of Swift 4+ It’s just: test1.count for reasons. (Thanks to Martin R) As of Swift 2: With Swift 2, Apple has changed global functions to protocol extensions, extensions that match any type conforming to a protocol. Thus the new syntax is: test1.characters.count (Thanks to JohnDifool for the heads up) As of Swift 1 Use … Read more

How can I disable the UITableView selection?

All you have to do is set the selection style on the UITableViewCell instance using either: Objective-C: cell.selectionStyle = UITableViewCellSelectionStyleNone; or [cell setSelectionStyle:UITableViewCellSelectionStyleNone]; Swift 2: cell.selectionStyle = UITableViewCellSelectionStyle.None Swift 3 and 4.x: cell.selectionStyle = .none Further, make sure you either don’t implement -tableView:didSelectRowAtIndexPath: in your table view delegate or explicitly exclude the cells you want … Read more

Xcode – How to fix ‘NSUnknownKeyException’, reason: … this class is not key value coding-compliant for the key X” error?

You may have a bad connection in your xib. I’ve had this error many times. While TechZen’s answer is absolutely right in this case, another common cause is when you change the name of a IBOutlet property in your .h/.m which you’ve already connected up to File’s Owner in the nib. From your nib: Select … Read more

How do I sort an NSMutableArray with custom objects in it?

Compare method Either you implement a compare-method for your object: – (NSComparisonResult)compare:(Person *)otherObject { return [self.birthDate compare:otherObject.birthDate]; } NSArray *sortedArray = [drinkDetails sortedArrayUsingSelector:@selector(compare:)]; NSSortDescriptor (better) or usually even better: NSSortDescriptor *sortDescriptor; sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@”birthDate” ascending:YES]; NSArray *sortedArray = [drinkDetails sortedArrayUsingDescriptors:@[sortDescriptor]]; You can easily sort by multiple keys by adding more than one to … Read more

How can I check for an active Internet connection on iOS or macOS?

Important: This check should always be performed asynchronously. The majority of answers below are synchronous so be careful otherwise you’ll freeze up your app. Swift Install via CocoaPods or Carthage: https://github.com/ashleymills/Reachability.swift Test reachability via closures let reachability = Reachability()! reachability.whenReachable = { reachability in if reachability.connection == .wifi { print(“Reachable via WiFi”) } else { … Read more

tech