What is the entry point of swift code execution?

The entry point in a plain Swift module is the file in the module called main.swift. main.swift is the only file which is allowed to have expressions and statements at the top level (all other Swift files in the module can only contain declarations). Cocoa Touch uses the @UIApplicationMain attribute on an implementation of UIApplicationDelegate … Read more

Sizing a UILabel to fit?

I had to do this enough that I extended UILabel to do it for me: @interface UILabel (BPExtensions) – (void)sizeToFitFixedWidth:(CGFloat)fixedWidth; @end @implementation UILabel (BPExtensions) – (void)sizeToFitFixedWidth:(CGFloat)fixedWidth { self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, fixedWidth, 0); self.lineBreakMode = NSLineBreakByWordWrapping; self.numberOfLines = 0; [self sizeToFit]; } @end then to have a label to have a variable multiline height but … Read more

What is the most robust way to force a UIView to redraw?

The guaranteed, rock solid way to force a UIView to re-render is [myView setNeedsDisplay]. If you’re having trouble with that, you’re likely running into one of these issues: You’re calling it before you actually have the data, or your -drawRect: is over-caching something. You’re expecting the view to draw at the moment you call this … Read more

Persisting Cookies In An iOS Application?

You shouldn’t need to persist the cookies yourself as suggested in the other answer. NSHTTPCookieStorage will persist the cookies for you but you need to ensure that the cookies have an expiry date set on the server-side. Cookies without an expiry date are considered ‘session only’ and will get cleared when you restart the app. … Read more

Fix warning “Capturing [an object] strongly in this block is likely to lead to a retain cycle” in ARC-enabled code

Replying to myself: My understanding of the documentation says that using keyword block and setting the variable to nil after using it inside the block should be ok, but it still shows the warning. __block ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:… [request setCompletionBlock:^{ NSDictionary *jsonDictionary = [[CJSONDeserializer deserializer] deserialize:request.responseData error:nil]; request = nil; // …. … Read more

What are best practices for validating email addresses on iOS 2.0

The answer to Using a regular expression to validate an email address explains in great detail that the grammar specified in RFC 5322 is too complicated for primitive regular expressions. I recommend a real parser approach like MKEmailAddress. As quick regular expressions solution see this modification of DHValidation: – (BOOL) validateEmail: (NSString *) candidate { … Read more

How to zoom in/out an UIImage object when user pinches screen?

As others described, the easiest solution is to put your UIImageView into a UIScrollView. I did this in the Interface Builder .xib file. In viewDidLoad, set the following variables. Set your controller to be a UIScrollViewDelegate. – (void)viewDidLoad { [super viewDidLoad]; self.scrollView.minimumZoomScale = 0.5; self.scrollView.maximumZoomScale = 6.0; self.scrollView.contentSize = self.imageView.frame.size; self.scrollView.delegate = self; } You … Read more

Is it possible to prevent an NSURLRequest from caching data or remove cached data following a request?

Usually it’s easier to create the request like this NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60.0]; Then create the connection NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self]; and implement the connection:willCacheResponse: method on the delegate. Just returning nil should do it. – (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse { return nil; }

tech