惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
S
Security Affairs
P
Palo Alto Networks Blog
Webroot Blog
Webroot Blog
P
Privacy International News Feed
H
Hacker News: Front Page
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
Cybersecurity and Infrastructure Security Agency CISA
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
The Blog of Author Tim Ferriss
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Schneier on Security
Schneier on Security
Jina AI
Jina AI
Simon Willison's Weblog
Simon Willison's Weblog
Scott Helme
Scott Helme
A
About on SuperTechFans
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recorded Future
Recorded Future
F
Fortinet All Blogs
PCI Perspectives
PCI Perspectives
Google DeepMind News
Google DeepMind News
T
Troy Hunt's Blog
MyScale Blog
MyScale Blog
I
InfoQ
F
Full Disclosure
宝玉的分享
宝玉的分享
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Security Archives - TechRepublic
Security Archives - TechRepublic
Latest news
Latest news
W
WeLiveSecurity
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
N
Netflix TechBlog - Medium
量子位
P
Proofpoint News Feed
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
GbyAI
GbyAI
SecWiki News
SecWiki News
AI
AI
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
G
GRAHAM CLULEY
N
News and Events Feed by Topic
K
Kaspersky official blog
V2EX - 技术
V2EX - 技术

isaced

使用 TypeScript 构建的 appstore-connect-sdk 归档 我用 SwiftUI 写了一个 V2ex 客户端 Docker 容器化多应用部署 Swift Server-side SDWebImage 为什么无法读取 webpmux 生成的 WebP 动图? [译] 探索 Swift 4 中新的 String API 对一个 Django 站点做 ab 压力测试 fir-mac 开发笔记 iOS10 Safari 引导用户信任企业签名 Swift Web 开发之 Vapor - 模版 Leaf(三) Swift Web 开发之 Vapor - 路由(二) HoloLens 初体验 Swift Web 开发之 Vapor - 入门(一) Jenkins for iOS 搭建日记 iOS HOOK 注入与重签名 Xcode 使用 Configuration 来配置不同的项目环境 图床从SAE迁移到七牛 给 UIProgressView 脱掉那层微弱的渐变 初用 CocoaAsyncSocket 记一次 SQLite 性能优化 有多色 - 开发小记 海岛风情 - 泰国普吉岛六日游 EGOCache 源码解析 日语五十音假名临摹校验算法 [iOS] UITextView 追加文字自动跳到顶部解决方法 Cocoa 新的依赖管理工具:Carthage 新年快乐! 博客主机换到搬瓦工 写了个iOS小游戏:有多色 iOS 后台任务之 Long-running background task 悄悄的就必须用 XCode 5.1.1 上传了! HTTP Content-type 与 AFNetworking Objective-Cloud 初体验(1) 掌中站上架小记 NSLoger 上线了 Python Markdown 做语法限制 Nginx + Gunicorn + Django 部署小记 用Shell脚本批量裁剪App Icon各个尺寸 LaunchRocket使用小记 用宏提速NSCoding 2014新年伊始:近期读过的好文推荐 Apache + Flask + mod_wsgi + Koding 部署小记 收集几个Objective-C的HTML解析库 NSMapTable: 不只是一个能放weak指针的 NSDictionary 2013,再见! Emlog到Typecho的迁移工具:emlog2typecho [iOS]给上拉下拉刷新PullToRefresh做本地化 [iOS]代码推荐:大图缩放视图 - Vertigo iOS:hidesBottomBarWhenPushed的正确用法 Xcode 5新的Interface Builder,你准备好了吗? [翻译]如何使用Django创建简单的博客 我的Mac音乐播放器 初识 NSDataDetector Objective-C 计算代码运行时间 WordPress上传文件自动重命名 iOS:仿jing.fm的音乐播放视图 我的啃黑苹果之路 关于 搜索 项目
[iOS] stringWithFormat 和 initWithFormat 有何不同?
2014-01-22 · via isaced

都说 Objective-C 是一门入门简单,深入难的语言,一直觉得基础不是很牢固,所以这几天也反复在看 Objective-C 内存管理、ARC 相关的东西,下一步准备看看 OC 的 Runtime 相关。

NSString

NSString 作为 Objective-C 的最基本的字符串类,从最开始接触 iOS 开发就用过它,那今天说的 NSString 中的 Format 实例化方法,也类似于 C 语言中的 printf 中的 format 格式化。昨天在看《10 iPhone Memory Management Tips》一文的时候看到文中为了提高效率用 initWithFormat 代替 stringWithFormat,今天详细看看这两个方法:

一个类方法,一个实例方法,仅仅是用起来可以少些两个字吗?

For Example

下面来个 For Example,在非 ARC 环境下,循环构造 10w 个 NSString,内容一样:

  • 首先是stringWithFormat,占用内存 5.2M
    for (int i = 0; i < 100000; i++) {
        [NSString stringWithFormat:@"%d",1234567890];
    }
  • 然后加上@autoreleasepool,占用内存 520KB
    for (int i = 0; i < 100000; i++) {
        @autoreleasepool {
            [NSString stringWithFormat:@"%d",1234567890];
        }
    }
  • 再来试试initWithFormat,占用内存 3.6MB
    for (int i = 0; i < 100000; i++) {
        [[NSString alloc] initWithFormat:@"%d",1234567890];
    }
  • 再加上autorelease尾巴和autoreleasepool,占用内存 520KB
    for (int i = 0; i < 100000; i++) {
        @autoreleasepool {
            [[[NSString alloc] initWithFormat:@"%d",1234567890] autorelease];
        }
    }

结论

  • + stringWithFormat: 类方法,返回一个 autorelease 的 NSString 实例,不用手动 Release,在自动释放池中会自动释放。

  • – initWithFormat: 实例方法,返回一个自己 Alloc 申请内存的 NSString 实例,根据 OC 内存管理黄金法则,管杀管埋,它则需要自己手动 Release。

因为这两个方法只是在没有使用 ARC 的时候有所不同,一个需要手动 Release 一个则是自动进入 autoreleasepool,所以在使用 ARC 的时候他们俩几乎没有什么区别。

参考资料: