ios - String is not convertible to Range<I> swift anyone 错误

标签 ios string swift range

 func setupAvatarColor(name: String, incoming: Bool) {
        let diameter = incoming ? UInt(collectionView.collectionViewLayout.incomingAvatarViewSize.width) : UInt(collectionView.collectionViewLayout.outgoingAvatarViewSize.width)

        let rgbValue = name.hash
        let r = CGFloat(Float((rgbValue & 0xFF0000) >> 16)/255.0)
        let g = CGFloat(Float((rgbValue & 0xFF00) >> 8)/255.0)
        let b = CGFloat(Float(rgbValue & 0xFF)/255.0)
        let color = UIColor(red: r, green: g, blue: b, alpha: 0.5)

        **let nameLength = count(name);**
        let initials : String? = name.substringToIndex(advance(sender.startIndex, min(3, nameLength)))
        let userImage = JSQMessagesAvatarFactory.avatarWithUserInitials(initials, backgroundColor: color, textColor: UIColor.blackColor(), font: UIFont.systemFontOfSize(CGFloat(13)), diameter: diameter)

        avatars[name] = userImage
    }


I receiving the error '`String is not convertible to Range<I>'in the highlighted code (10th line)`. AnyOne?

最佳答案

在 Swift 2 中,Apple 删除了很多全局函数。

在您的情况下,要获取字符串的长度,请执行以下操作:

str.characters.count

关于ios - String is not convertible to Range<I> swift anyone 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33146487/

相关文章:

iPhone iOS 如何从相机胶卷图像中提取照片元数据和地理标记信息?

ios - 使用 UIPanGestureRecognizer 旋转 ImageView,错误——Swift

ios - 如何从数组中的对象调用方法?

iphone - 为什么要使用@properties?

ios - 在 TableView 行中动态对齐 UITextView

Java string.equals 不等式函数?

javascript - 从类返回字符串 - JavaScript

python - 如何在 Python 字符串模板类上转义 $?

ios - iPad 模拟器正常但 iPad 在使用 WKWebview 上传图像时崩溃应用程序

swift - 如何在 swift ui 的导航 View 中的大标题下方添加副标题?