Reputation: 13
func fontForDisplay(atIndexPath indexPath : NSIndexPath) -> UIFont? {
if indexPath.section == 0 {
let familyName = familyNames[indexPath.row]
let fontName = UIFont.fontNamesForFamilyName(familyName).first as String!
return UIFont(name: fontName, size: cellPointSize)
}else {
return nil
}
}
Error Message : FontName is nil
But I don't know how to solve it
I need help. Thanks
Upvotes: 1
Views: 674
Reputation: 286
You can use guard. It allows you to focus on valid statements and to use optional binding for fontName :
func fontForDisplay(atIndexPath indexPath : NSIndexPath) -> UIFont? {
guard indexPath.section == 0 else { return nil }
let familyName = familyNames[indexPath.row]
guard let fontName = UIFont.fontNamesForFamilyName(familyName).first else { return nil }
return UIFont(name: fontName, size: cellPointSize)
}
Upvotes: 1
Reputation: 13
When i got this error it was because there is an IBObject that has not been called into the coreesponding viewcontroller .swift.
i would suggest you ctrl-click on all objects and should bring up a black menu, with all objext association in, sometime you can have 2 object calls to the .swift file on the same IBObject, and although you have deleted it in the code, it still thinks that it is there in the storyboard!
i hope that this helps
Upvotes: 0
Reputation: 4337
Try this:
func fontForDisplay(atIndexPath indexPath : NSIndexPath) -> UIFont? {
if indexPath.section == 0 {
let familyName = familyNames[indexPath.row]
if let fontName = UIFont.fontNamesForFamilyName(familyName).first as String! {
return UIFont(name: fontName, size: cellPointSize)
} else {
return nil
}
}else {
return nil
}
}
Upvotes: 0