【Swift】iOS开发小坑历险记(一)
KristenShar
9年前
<h2><strong>前言</strong></h2> <p> 边开发边学习,边攒经验,汇总一下记录到这里</p> <h2>1、隐藏/显示密码功能</h2> <p> 光设置secureTextEntry还不行,你会发现UITextField在切换到显示密码时会多一个空字符,看着巨别扭,需要在更改secureTextEntry后进行如下设置:</p> <p> let pwd = psdField.text<br> self.psdField.text = pwd + " "<br> self.psdField.text = pwd</p> <h2>2、获取当前类的名称</h2> <p>String.fromCString(object_getClassName(self))</p> <p> 注意:通过_stdlib_getDemangledTypeName也能取到,但是如果在父类里面去就只能取到父类的名称</p> <h2>3、 国际化</h2> <p>find . \( -name '*.m' -o -name '*.h' \) -print0 | xargs -0 genstrings -o en.lproj </p> <p> 凡是使用了NSLocalizedString的字符串都能被找到,支持子目录查找,注意替换en.lproj</p> <h2>4、UITableView分割线的显示问题</h2> <p> 去掉分割线:设置UITableView的separatorStyle = UITableViewCellSeparatorStyle.None</p> <p> 去掉多余的分割线:设置UITableView的tableFooterView = UIView() (要是不设置会很丑,不管有没有数据都会显示分割线)</p> <p> 处理 iOS8 分割线左边距设置为0失效的问题,参考这里(http://stackoverflow.com/questions/25770119/ios-8-uitableview-separator-inset-0-not-working):</p> <pre> <code class="language-swift"> func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) { // Remove seperator inset if cell.respondsToSelector("setSeparatorInset:") { cell.separatorInset = UIEdgeInsetsZero } // Prevent the cell from inheriting the Table View's margin settings if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:") { cell.preservesSuperviewLayoutMargins = false } // Explictly set your cell's layout margins if cell.respondsToSelector("setLayoutMargins:") { cell.layoutMargins = UIEdgeInsetsZero } } </code></pre> <h2>5、 格式化数字输出 K/M</h2> <pre> <code class="language-swift">extension String { public func substring(startIndex: Int, endIndex: Int) -> String{ return (self as NSString).substringWithRange(NSRange(location: startIndex, length: endIndex - startIndex)) } } public static func prettyNumber(num: Double) -> String{ if (num < 10000) { return "\(Int(num))"; } else if (num < 100000) { return "\(num / 1000.0)".substring(0, endIndex: 4) + "K" } else if (num < 1000000) { return "\(num / 1000.0)".substring(0, endIndex: 3) + "K" } else if (num < 100000000) { return "\(num / 1000000.0)".substring(0, endIndex: 4) + "M" } else if (num < 1000000000) { return "\(num / 1000000.0)".substring(0, endIndex: 3) + "M" } else if (num < 100000000000) { return "\(num / 1000000000.0)".substring(0, endIndex: 4) + "M" } else if (num < 1000000000000) { return "\(num / 1000000000.0)".substring(0, endIndex: 3) + "M" } return "INF"; }</code></pre> <h2>6、 判断屏幕是否是横屏</h2> <pre> <code class="language-swift"> public static func isIsLandscape() -> Bool { return UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) || UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.LandscapeLeft || UIApplication.sharedApplication().statusBarOrientation == UIInterfaceOrientation.LandscapeRight }</code></pre> <h2>7、 URL 编码</h2> <pre> <code class="language-swift">text.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())</code></pre> <p>这个 text 的类型是 String ,常用于搜索功能,在 URL 中包含被搜的关键字,如果不处理搜中文或者带空格的英文会直接崩溃</p> <p>来源:<a href="/misc/goto?guid=4959672200998202294">农民伯伯</a></p>