How to generate a random number in Swift?

Swift 4.2+ Swift 4.2 shipped with Xcode 10 introduces new easy-to-use random functions for many data types. You can call the random() method on numeric types. let randomInt = Int.random(in: 0..<6) let randomDouble = Double.random(in: 2.71828…3.14159) let randomBool = Bool.random()

how to implement lazy loading of images in table view using swift

Old Solution: Since you doesn’t show any code. Here is the example for you. func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { // try to reuse cell let cell:CustomCell = tableView.dequeueReusableCellWithIdentifier(“DealCell”) as CustomCell // get the deal image let currentImage = deals[indexPath.row].imageID let unwrappedImage = currentImage var image = self.imageCache[unwrappedImage] let imageUrl = NSURL(string: … Read more

[NSObject : AnyObject]?’ does not have a member named ‘subscript’ error in Xcode 6 beta 6

As mentioned in the Xcode 6 beta 6 release notes, a large number of Foundation APIs have been audited for optional conformance. These changes replace T! with either T? or T depending on whether the value can be null (or not) respectively. notification.userInfo is now an optional dictionary: class NSNotification : NSObject, NSCopying, NSCoding { … Read more

SwiftUI List color background

iOS 16 Since Xcode 14 beta 3, You can change the background of all lists and scrollable contents using this modifier: .scrollContentBackground(.hidden) You can pass in .hidden to make it transparent. So you can see the color or image underneath. iOS 15 and below All SwiftUI’s Lists are backed by a UITableView (until iOS 16). … Read more

Adding Navigation Bar programmatically iOS

-(void)ViewDidLoad { UINavigationBar* navbar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)]; UINavigationItem* navItem = [[UINavigationItem alloc] initWithTitle:@”karthik”]; // [navbar setBarTintColor:[UIColor lightGrayColor]]; UIBarButtonItem* cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(onTapCancel:)]; navItem.leftBarButtonItem = cancelBtn; UIBarButtonItem* doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(onTapDone:)]; navItem.rightBarButtonItem = doneBtn; [navbar setItems:@[navItem]]; [self.view addSubview:navbar]; } -(void)onTapDone:(UIBarButtonItem*)item{ } -(void)onTapCancel:(UIBarButtonItem*)item{ } Swift3 let navigationBar … Read more

Custom Alert (UIAlertView) with swift

Code tested in Swift 5 and Xcode 10 How to make your own custom Alert I was wanting to do something similar. First of all, UIAlertView is deprecated in favor of UIAlertController. See this answer for the standard way to display an alert: How would I create a UIAlertView in Swift? And both UIAlertView and … Read more