专业java、php、iOS、C++、网页设计、平面设计、网络营销、游戏开发、前端与移动开发培训机构
来源处理
在
HMStatusViewModel
定义微博来源
属性statusSource: String?
,用于保存截取好的微博来源,在init(status: HMStatus)
里面截取微博来源/// 一个HMStatusViewModel包含一个微博模型 class HMStatusViewModel: NSObject { // MARK: - 属性 /// 微博模型 var status: HMStatus ... /// 微博来源 var statusSource: String? init(status: HMStatus) { self.status = status // 设置是否有被转发微博 isRetweetStatus = status.retweeted_status != nil // 微博日期描述 sinaDateDescription = Date.dateDescription(fromSinaDateString: status.created_at ?? "一万年") // 微博来源 // 设置来源:<a href=\"http://weibo.com/\" rel=\"nofollow\">微博 weibo.com</a> if let source = status.source, source.characters.count > 0 { let nsSource = source as NSString let firstRange = nsSource.range(of: ">") let secondRnage = nsSource.range(of: "</") print("firstRange: \(firstRange.location), \(firstRange.length), secondRnage: \(secondRnage.location), \(secondRnage.length)") let range = NSRange(location: firstRange.location + 1, length: secondRnage.location - firstRange.location - 1) let newSource = nsSource.substring(with: range) statusSource = "来自 \(newSource)" } else { statusSource = "未知来源" } ... } }