Hi, I am having a problem getting the Contacts app to run when I click on the add + button. I get the following error messages;
2017-10-27 15:42:33.335036+0100 Contacts[41021:3407855] [MC] Lazy loading NSBundle MobileCoreServices.framework
2017-10-27 15:42:33.335664+0100 Contacts[41021:3407855] [MC] Loaded MobileCoreServices.framework
2017-10-27 15:42:33.350694+0100 Contacts[41021:3407855] [MC] System group container for systemgroup.com.apple.configurationprofiles path is /Users/StephenLearmonth/Library/Developer/CoreSimulator/Devices/A0EB4B24-60D5-4029-B217-A0B0A61C4163/data/Containers/Shared/SystemGroup/systemgroup.com.apple.configurationprofiles
fatal error: unexpectedly found nil while unwrapping an Optional value
2017-10-27 15:42:33.354515+0100 Contacts[41021:3407855] fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
Here is my Swift code in NewContactViewController.swift;
// NewContactViewController.swift
// Contacts
//
// Created by Stephen Learmonth on 25/10/2017.
// Copyright © 2017 BigNerdRanch. All rights reserved.
//
import UIKit
class NewContactViewController: UIViewController {
@IBOutlet var firstNameTextField: UITextField!
@IBOutlet var lastNameTextField: UITextField!
@IBOutlet var contactImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view
contactImageView.image = ImageFactory.generateDefaultImage(of: contactImageView.frame.size)
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
and here is my Objective-C code for ImageFactory.m
// ImageFactory.m
// Contacts
//
// Created by Stephen Learmonth on 27/10/2017.
// Copyright © 2017 BigNerdRanch. All rights reserved.
//
#import "ImageFactory.h"
@implementation ImageFactory
+ (UIImage *)generateDefaultImageOfSize:(CGSize)size
{
//Make Frame and get context
CGRect frame = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContext(size);
CGContextRef context = UIGraphicsGetCurrentContext();
// Draw white background and yellow circle
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextFillRect(context, frame);
CGContextSetFillColorWithColor(context, [[UIColor yellowColor] CGColor]);
CGContextFillEllipseInRect(context, frame);
CGFloat x = frame.origin.x + size.width / 2;
CGFloat y = frame.origin.y + size.height / 2;
CGPoint center = CGPointMake(x, y);
// Draw eyes and smile
CGSize eyeSize = CGSizeMake(frame.size.width * 0.1, frame.size.height * 0.1);
CGFloat separation = frame.size.width * 0.1;
CGPoint leftPt = CGPointMake(center.x - (separation + eyeSize.width),
center.y - (eyeSize.height * 2));
CGPoint rightPt = CGPointMake(center.x + separation,
center.y - (eyeSize.height * 2));
CGRect leftEye = CGRectMake(leftPt.x, rightPt.y, eyeSize.width, eyeSize.height);
CGRect rightEye = CGRectMake(rightPt.x, rightPt.y, eyeSize.width, eyeSize.height);
CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
CGContextFillEllipseInRect(context, leftEye);
CGContextFillEllipseInRect(context, rightEye);
CGFloat smileHeight = center.y + eyeSize.height;
CGPoint leftEdge = CGPointMake(leftEye.origin.x, smileHeight);
CGPoint rightEdge = CGPointMake(rightEye.origin.x + rightEye.size.width, smileHeight);
CGFloat smileLength = rightEdge.x - leftEdge.x;
CGFloat smileWidth = eyeSize.width / 3;
CGContextSetLineWidth(context, smileWidth);
CGContextBeginPath(context);
CGContextMoveToPoint(context, leftEdge.x, leftEdge.y);
CGContextAddCurveToPoint(context, leftEdge.x + (smileLength / 4),
smileHeight + smileWidth * 2, rightEdge.x - (smileLength / 4),
smileHeight + smileWidth * 2, rightEdge.x, rightEdge.y);
CGContextStrokePath(context);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
Solved the problem! I hadn’t connected the UIImageView object to its swift code.