我正在尝试编写一个函数来向K和M提供数千和数百万例如:
1000 = 1k
1100 = 1.1k
15000 = 15k
115000 = 115k
1000000 = 1m
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止的地方:
func formatPoints(num: Int) -> String {
let newNum = String(num / 1000)
var newNumString = "\(num)"
if num > 1000 && num < 1000000 {
newNumString = "\(newNum)k"
} else if num > 1000000 {
newNumString = "\(newNum)m"
}
return newNumString
}
formatPoints(51100) // THIS RETURNS 51K instead of 51.1K
Run Code Online (Sandbox Code Playgroud)
如何让这个功能起作用,我缺少什么?
我正在尝试在FriendsTableView(UITableView)单元格中异步加载图片.图像加载正常,但是当我滚动表格时,图像会改变几次,错误的图像会分配给错误的单元格.
我已经尝试了我在StackOverflow中可以找到的所有方法,包括向raw添加一个标签,然后检查它但是没有用.我还要验证应该使用indexPath更新的单元格并检查单元格是否存在.所以我不知道为什么会这样.
这是我的代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("friendCell", forIndexPath: indexPath) as! FriendTableViewCell
var avatar_url: NSURL
let friend = sortedFriends[indexPath.row]
//Style the cell image to be round
cell.friendAvatar.layer.cornerRadius = 36
cell.friendAvatar.layer.masksToBounds = true
//Load friend photo asyncronisly
avatar_url = NSURL(string: String(friend["friend_photo_url"]))!
if avatar_url != "" {
getDataFromUrl(avatar_url) { (data, response, error) in
dispatch_async(dispatch_get_main_queue()) { () -> Void in
guard let data = data where error == nil else { return }
let thisCell …
Run Code Online (Sandbox Code Playgroud) 所以我试图在一个特定范围内得到一系列日期数,符合
ISOWeekday
(返回1-7,其中1表示星期一,7表示星期日)
因此,如果工作周从星期日(7
)开始,到星期二()结束4
,
我需要输出这个数组:
7,1,2,3,4
const startWeek = 7;
const endWeek = 4;
for (let i = 1; i < 7; i++) {
if (i > startWeek && i < endWeek) {
console.log(i);
}
}
Run Code Online (Sandbox Code Playgroud)
ios ×2
swift ×2
arrays ×1
asynchronous ×1
datetime ×1
format ×1
image ×1
javascript ×1
numbers ×1
swift2 ×1
uitableview ×1