Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Swift错误:在展开可选值时意外发现nil_Ios_Swift - Fatal编程技术网

Ios Swift错误:在展开可选值时意外发现nil

Ios Swift错误:在展开可选值时意外发现nil,ios,swift,Ios,Swift,错误消息:FontName为nil 但我不知道怎么解决它 我需要帮助。谢谢试试这个: func fontForDisplay(atIndexPath indexPath : NSIndexPath) -> UIFont? { if indexPath.section == 0 { let familyName = familyNames[indexPath.row] let fontName = UIFont.fontNamesForFamilyNa

错误消息:FontName为nil
但我不知道怎么解决它

我需要帮助。谢谢

试试这个:

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
    }
}

当我遇到这个错误时,是因为有一个IBObject没有被调用到对应的viewcontroller.swift中

我建议您按住ctrl键单击所有对象,并应显示一个黑色菜单,其中包含所有objext关联,有时您可以在同一IBObject上对.swift文件进行两次对象调用,尽管您已在代码中删除了它,但它仍认为它在情节提要中


我希望这能帮助你使用guard。它允许您关注有效语句,并对fontName使用可选绑定:

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
   }
}

请检查您的familyNames数组,我猜familyName为零。请显示此familyNames数组的内容。感谢您的关注:)谢谢您的关注:)
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)
}