Swift
文章平均质量分 50
czjuttsw
Learning, staying up to date, and working on the latest and greatest in languages and APIs is what keeps me excited.
展开
-
Swift中的集合类型
一.引子: 在TIOBE编程语言排行榜中,Swift位居第18位,从2014WWDC发布会首次公布至今不到半年时间,swift一直受到编程人员的追捧,其热衷程度并不亚于当红巨星Taylor Swift。相信在不远的将来,swift能够平稳发展,并逐步取代Objective-C。二.swift的集合类型 下面回归主题。作为一名iOS开发者,我们已经非常熟悉诸如原创 2014-10-06 20:15:11 · 5921 阅读 · 0 评论 -
如何解决警告linking against dylib not safe for use in application extensions
因为app extension限制了某些API的使用, ( App Extensions不能使用的一些API ) ,因此在自定义自己的framework后,这个framework可能包含了某些在App Extensions里不能使用的API,因此为了安全起见才会给出这个警告。下面是清除警告的方法:选中自定义framework的target(这里的例子是DarwinKit)原创 2014-12-22 17:45:30 · 4128 阅读 · 0 评论 -
AppleWatch如何打开Glance
步骤一:打开Edit Scheme...步骤二:在弹出的对话框中,左边的菜单选中“Run",点击右边的Executable下拉框,选择Glance 开头的选项,然后单击Close关闭对话框.步骤三:选择WatchKit App Scheme,点击Run按钮。如果没有出现任何问题的话,Glance会是这个样子的:这是原创 2014-12-23 10:55:47 · 2323 阅读 · 0 评论 -
如何读取代码行数
使用 CLOC (Count Line of Code)前提条件:1. 预装有perl 2. 下载CLOC. http://cloc.sourceforge.net/解压安装包,得到cloc-version.pl打开命令行,定位到解压文件的文件夹,然后敲入命令perl cloc-version.pl YourProject.tar.gz原创 2015-01-13 18:58:51 · 4713 阅读 · 0 评论 -
如何让App Extension访问Cocoapods引入的第三方库
步骤一:选中项目,点击App Extension target(这里是Today Extension)步骤二:在右边的Tabs中,选择Build Settings,在过滤框中输入"bridg"在弹出的输入框中,输入你的桥接文件名(这个文件是将Objective-C写的文件暴露给Swfit使用,如果没有这个文件名,可以新建一个头文件,名字随意,一般命名规则是项原创 2014-12-28 21:21:01 · 5276 阅读 · 4 评论 -
如何在Swift中创建和使用Model [ MagicalRecord ]
步骤一:Command + N - > iOS -> Core Data -> Data Model.步骤二:打开xcdatamodelId,创建一个Entity和属性。步骤三:选中创建的Entity,在右边的面板中选择第三个Tab(Data Model Inspector),在class一栏中添加项目名前缀。(ProjectName.EntityName)步骤四:创原创 2014-12-28 22:09:28 · 3679 阅读 · 0 评论 -
Xcode的archive菜单选项是灰色的
如果当前Xcode选择的设备是模拟器,那么"Archive"是灰色的,不能点。把当前设备设置成连接的真机设备或者默认的"iOS Device","Archive"会变成黑色。原创 2015-04-21 16:35:30 · 22525 阅读 · 0 评论 -
获取iPhone或者iPad的UDID
1.连接iPhone或者iPad到电脑。2. 打开iTunes。3. 在Summary(概要)右边点击序列号,Serial Number会变成UDID(如果不是UDID,请继续点击)。4. command+c(MAC) 或者 Ctrl+c(Windows)复制UDID。5. 粘贴到你需要的地方。 (比如到Member Center的device里。)参考原创 2015-04-21 16:23:59 · 1869 阅读 · 0 评论 -
Xcode查看真机app沙盒内容
1. 打开Xcode2. 连接iPhone或者iPad3. 打开菜单Window-> Devices4. 在DEVICES选择连接的真机5. 在右侧的Installed Apps选择你要查看的app6. 点击下面的齿轮按钮,选择Download Container,保存到桌面,生成XXX.xcappdata文件7. 在桌面找到该文件,右键以包(package)的形式打开文件原创 2015-04-29 10:22:43 · 11204 阅读 · 0 评论 -
如何将Swift中的多参函数转化成Curring函数
假设我们有一个含有多个参数的函数,用于构建一个人的基本信息:func buildInfoWithName(name: String,#age: Int,#gender: String,#address: String,#phone: String) -> String { return "My name is " + name + ",I'm a " + g原创 2015-04-30 10:31:02 · 1232 阅读 · 0 评论 -
本地化app名字
How to Localize App NamesIt is possible to localize app names directly within Xcode and you do not need to create multiple versions of the same app. The great and wonderful InfoPlist.str转载 2015-05-07 13:56:16 · 2692 阅读 · 0 评论 -
如何在Swift中使用NSError
步骤一:声明NSError变量。一定要加"?",不加或者加"!"都不行。因为使用了optional,所以要用var而不用let。var error: NSError?步骤二:使用的时候在变量前加上"&"。NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers, error: &error)之原创 2015-07-09 17:33:14 · 4672 阅读 · 0 评论 -
Swift API Availability Checking
在开发的过程中,难免我们会用到新的API,如果处理不当的话,对于低于该版本的设备在运行该应用的时候可能会奔溃,为了防止出现这种情况,我们需要在运行时判断API是否可用。iOS9中引进了新的View class,叫做UIStackView,它是一个View容器(可分为水平布局和垂直布局),与其他View不同的是,该View会自动管理内部view的布局,而无须编码实现自动布局,同时细心的你会发现,原创 2015-06-13 22:10:44 · 1988 阅读 · 0 评论 -
Storyboard Reference
在某些情况下,你开发的应用可能包含有各种各样的复杂界面,如果你使用Storyboard来管理这些界面,通常你会将这些界面按照相应的逻辑分成许多子模块,放在不同的storyboard里,比如注册相关的放到Register.Storyboard,图片相关的放到Photos.Storyboard,而主界面放到Main.Storbyoard。这么做的好处是不仅可以缩小每个storbyoard的打开速度,而原创 2015-06-12 23:01:30 · 2668 阅读 · 0 评论 -
UIImagePickerController本地化控件文字
在使用UIImagePickerController时候,你会发现在选择照片或者拍照的时候,界面的许多控件都是英文的,比如"Cancel","Choose"等。那么如何将它们本地化呢,比如替换成“取消”和“选取“?甚至你调用系统其它库(比如ALAssetsLibrary)返回的各种字符串都是英文的。解决这个的方案是往Info.plist里添加key/value。CFBundleDevelo原创 2015-07-04 10:10:37 · 3149 阅读 · 0 评论 -
改变状态栏和导航栏的透明度
步骤一:为UIImage类添加静态方法:extension UIImage { static func imageWithColor(color: UIColor) -> UIImage { let rect = CGRect(x: 0, y: 0, width: 1, height: 1) UIGraphicsBegin原创 2015-07-13 19:58:07 · 3137 阅读 · 0 评论 -
当自定义leftBarButtonItem(s)左边缘滑动失效时
如何开启:1.ViewController实现 UIGestureRecognizerDelegate 协议class YourViewController: UIGestureRecognizerDelegate { func viewDidLoad() { navigationItem.setLeftBarButtonItems([...,...]) //原创 2014-12-18 21:51:23 · 1674 阅读 · 0 评论 -
如何获取Info.plist文件信息
swift:NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"]比如: if let version = NSBundle.mainBundle().infoDictionary?["CFBundleShortVersionString"] as? String {原创 2014-12-18 21:44:23 · 4335 阅读 · 0 评论 -
@IBDesignable和@IBInspectable
最近一直在看苹果公司提供的两本swift官方教程电子书,一部是《The Swift Programming Language》,另一部是《Using Swift With Cocoa and Objective-C》。昨天正好看到第二部电子书的“Writing Swift Classes with Objective-C Behavior”这一节,其中讲述了关于实时渲染这一技术。下面是摘抄的其中一原创 2014-10-08 11:02:03 · 4725 阅读 · 0 评论 -
[swift]初始化方法自动继承
子类默认不会继承父类的初始化方法,然而,如果某种条件满足的话,父类的初始化方法还是可以继承给子类。在通常情况下,这意味着你不必复写父类的初始化方法,在安全的前提下可以以最低的代价继承父类的初始化方法。 假设子类新增的stored properties 都提供了默认值,那么提供了以下两条规则:规则1:如果你的子类没有定义任何的指定初始化方法(新增便利初始化方法可有可无),那么子类会翻译 2014-10-26 16:52:54 · 1873 阅读 · 0 评论 -
swift之mutating关键字
在swift中,包含三种类型(type): structure,enumeration,class其中structure和enumeration是值类型(value type),class是引用类型(reference type)但是与objective-c不同的是,structure和enumeration也可以拥有方法(method),其中方法可以为实例方法(instance原创 2014-09-30 12:02:20 · 12307 阅读 · 1 评论 -
如何在swift中使用cocoapods导入的第三方oc库
假如你来到这里,说明你已经开始着手使用swift这门新语言了。就像Java有Maven一样,Objective-C也有自己的依赖管理工具cocoapods。但是由于swift才出来不久,目前很多cocoapods管理的第三方库依然是由Objective-C编写的。为了能够在swift中使用这些类库,需要在Xcode中进行一些配置。假设你的项目是基于cocoapods的,并且原创 2014-09-30 14:23:59 · 5458 阅读 · 0 评论 -
Swift内置函数Reference
Swift语言提供了许多方便的内置函数,如countElements,join,max,reverse等。下面是一个清单,供以后查阅。http://swift-ios.co/standard-functions-in-swift/Standard functions in Swift June 17, 2014 admin UncategorizedSw转载 2014-10-29 00:19:58 · 1398 阅读 · 0 评论 -
Swift编程语言初探
继WWDC2014后,新的编程语言Swift浮出水面,它具有快速、现代、安全、可交互等特征,并且其语法简单,入门门槛低,有望替代语法复杂难懂的Objective-C语言。据其作者Chris Lattner在博客上声称,该语言开发只用了4年时间,并且底层框架基本上都是由其一人开发完成。 按照惯例,先用Swift来编写一个简单的"Hello World"println("Hello Wo原创 2014-06-05 15:05:13 · 21157 阅读 · 14 评论 -
App Store Review Guidlines
Apple DeveloperTechnologiesResourcesProgramsSupportMember CenterRecommended ResultsApp Store Review GuidelinesIntroductionWe're pleased that y转载 2014-10-30 23:55:49 · 2250 阅读 · 0 评论 -
Swift中如何传递Selector参数
Objective-C的@selector在Swift中用Selector结构体来替代。在Swift中,我们可以使用字符串常量来构造Selector,例如:let mySelector: Selector = "tappedButton:"因为字符串最终会被自动转换成Selector。下面是一个例子:// SwiftmyButton.addTarget(self, ac原创 2014-11-01 13:00:47 · 13230 阅读 · 1 评论 -
nil coalescing operator
nil coalescing operator ?? 就是 optional和 三元运算符?:的简写形式。例如一个optional String类型的变量var a:String?// println(a != nil ? a! : "shabi")println(a ?? "shabi") // shabi// a ?? "shabi" equals a原创 2014-10-17 10:35:48 · 1569 阅读 · 0 评论 -
Swift String length property
Swift的String竟然没有length属性,好难受,每次要获取String的字符串长度都要借助全局函数countElements。没办法,只有扩展String结构体,给它添加一个属性了。import Foundationextension String { // readonly computed property var length:原创 2014-10-11 19:20:11 · 5311 阅读 · 0 评论 -
如何在swift中自定义基本类型Bool
BooleanThe boolean Bool type in Swift underlies a lot of primitive functionality, making it an interesting demonstration of how to build a simple type. This post walks through the creation of a ne转载 2014-10-19 21:01:16 · 2951 阅读 · 0 评论 -
Swift迎来了1.0 GM 版(2014.09.09)
2014年6月2日,swift开发团队将swift语言公之于众。而2014年9月9日迎来了swift的第二个里程碑,swift1.0版本(GM),这意味着不管你的应用有一部分功能是用swift写的,还是整个应用是纯swift写的,你都可以将这个应用通过提交到苹果商店与全世界分享你的成果。你可能已经注意到我们使用了GM代号,而不是final,这是因为Swift还会持续添加新功能,改善性能和重定义原创 2014-10-19 18:29:14 · 978 阅读 · 0 评论 -
iOS 8自定义动画转场上手指南
http://www.appcoda.com/custom-segue-animations/转载 2015-01-27 10:48:56 · 806 阅读 · 0 评论 -
如何防止用户同时点击多个button
设置button的exclusiveTouch属性为true即可。btn.exclusiveTouch = true此属性默认为false原创 2014-12-18 21:54:43 · 1942 阅读 · 1 评论 -
ioss 8.1 to 8.2 api differences
UIKit ChangesUIKitUIApplication.hAdded -[UIApplicationDelegate application:handleWatchKitExtensionRequest:reply:]UILocalNotification.hAdded UILocalNotification.alertT原创 2014-12-19 00:15:05 · 752 阅读 · 0 评论 -
Content Hugging Priority & Content Compression Resistance Priority 区别
AUTO LAYOUT MAGIC: CONTENT SIZING PRIORITIESSeptember 15, 2015 by Hector MatosOne of my personal favorite things about good code and well-designed features is转载 2015-11-18 11:29:17 · 2502 阅读 · 0 评论