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?
In Swift 2 Apple removed alot of global functions.
In your case to get the length of a String do:
str.characters.count
You can't use count
anymore in Swift 2.0 for getting the string length.
So instead of:
let nameLength = count(name);
Use:
let nameLength = name.endIndex;
or
let nameLength = name.characters.count;
Swift 2.0 lets you call advance on endIndex and startIndex which is what you need here I guess. Ps, avataWithUserInitials takes NSString as first argument and substringToIndex returns String so I don't see any reason to define it as String optional.
let initials = name.substringToIndex((name.characters.count < 3) ? name.endIndex:name.startIndex.advancedBy(3))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.