- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
对于一个新的 Xcode 项目,我以这种方式添加了一个带有自定义 init 方法的新类:
import Cocoa
class NewClass: NSControl {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
但是,如果我构建并存档项目,源文件的绝对路径会在二进制文件中显示:
/Users/my_username/my_path_to_xcode_projects/new_project/NewClass.swift
我计划通过互联网发布我的期末项目。我怎样才能防止 Xcode 向其他人透露我的目录布局?我从类似问题的答案中了解到,在 Xcode 的“Build Settings”中,应该将“Deployment Prostprocessing”设置为 Yes,以便从二进制文件中去除符号,但这在我的情况下似乎不起作用。
最佳答案
如果你仔细观察 fatalError
的签名:
@noreturn public func fatalError(
@autoclosure message: () -> String = default,
file: StaticString = #file, line: UInt = #line)
您可以看到 #file
和 #line
默认参数。这些在编译时进行评估并解析为当前文件和行。
要不使用默认参数,您可以自己提供:
fatalError("Something went wrong", file: "", line: 0)
当它在发布配置上编译时,路径不包含在二进制文件中。
如果你在标准库中搜索#file
,你可以找到其他4个实例,其中两个是assert
和assertionFailure
不会包含在发布版本中。剩下的 2 个是 precondition
和 preconditionFailure
。
如果我们 add the DEBUG
/RELEASE
symbols :
我们可以对模拟的 fatalError
等函数进行条件编译,这些函数在发布版本中不使用 #file
预处理器语句:
#if RELEASE
@inline(__always) @noreturn func fatalError(@autoclosure message: () -> String = "") {
Swift.fatalError(message, file: "", line: 0)
}
@inline(__always) func preconditionAlt(@autoclosure condition: () -> Bool, @autoclosure _ message: () -> String = "") {
Swift.precondition(condition, message, file: "", line: 0)
}
@inline(__always) @noreturn func preconditionFailureAlt(@autoclosure message: () -> String = "") {
Swift.preconditionFailure(message, file: "", line: 0)
}
#else
@inline(__always) func preconditionAlt(@autoclosure condition: () -> Bool, @autoclosure _ message: () -> String = "", file: StaticString = #file, line: UInt = #line) {
Swift.precondition(condition, message, file: file, line: line)
}
@inline(__always) @noreturn func preconditionFailureAlt(@autoclosure message: () -> String = "") {
Swift.preconditionFailure(message, file: file, line: line)
}
#endif
您调用任何这些函数的所有实例现在都使用自定义实现。这样您在调试时仍然可以获得文件和行信息,但可以防止它出现在产品中。您必须将 precondition[Failure]
的每个实例更改为 precondition[Failure]Alt
,否则 Swift 无法明确地推断该函数。您可以按Shift-Alt-Cmd-F在整个项目中搜索和替换
如果第三方框架使用了这些函数,而你在自己的机器上编译,那么路径显然会包含在框架产品中。为防止这种情况,您要么必须使用预编译版本,要么使用模拟自行编译。
如果任何其他自声明或第三方函数使用 #file
,您显然也必须更改它,尽管这不是很常见。
可能还有其他我不知道的文件结构泄露的可能性。
关于swift - 如何防止自定义 init 方法在 Xcode 中泄露绝对源路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38535876/
大家晚上好! 在当前的项目中,我遇到了相当令人担忧的内存泄漏,但我似乎无法修复它。 我让应用程序在标准使用情况下运行过夜,当我在 8 小时后醒来时,它消耗了约 750MB 内存,而它一开始的内存约为
class MyViewController: UIViewController { @IBOutlet weak var webView: UIWebView? override
我的 sql 处理程序有问题 A SQLiteConnection object for database '/data/data/.../databases/queueManager' was le
我在引用 block 本身内的“NSBlockOperation”时遇到麻烦。我需要检查操作是否被取消,并且似乎在启用 ARC 的项目中运行时,对“searchOperation”的任何访问都会泄漏。
public class ProgressCircleActivity extends AppCompatActivity { private ProgressDialog progressB
Activity 泄漏是我可以为当前问题想到的最具体的术语。如果有其他情况,请指正。 场景:我创建了一个简单的 Android 测试应用程序来解决我的问题。我有一个 Activity ,一个添加到 f
我正在尝试创建身份验证系统,如果设备关闭 (SCREEN_OFF) 超过 INTERVAL,该系统会弹出登录窗口。 我已经注册了一个 BroadcastReceiver 来监听可启动 Activity
我想知道如果生产 key 被泄露需要采取哪些步骤。幸运的是,情况并非如此,但还是很高兴知道。 特别是,如果简单地将旧 key 交换为新生成的 key ,会发生什么情况?由于它用于生成哈希,是否会破坏整
我正在使用 Leak Canary 来跟踪内存泄漏,它说以下内容被泄漏: static hk.o references ht.a leaks MainActivity instance hk.o 和
-(NSDate *)dateFromDate:(NSDate *)inDate withNewTime:(NSDateComponents *)inTimeComponents { NSCalend
当我使用 AudioToolBox 播放音乐时,内存泄漏严重。 AVAudioPlayer *newMusicPlayer = [[AVAudioPlayer alloc] initWithData:
我使用 OpenAL 在我的应用程序中播放声音。当我使用 Instruments 工具测试它时,它发现了泄漏: LeakedObject = GeneralBlock-512 大小 = 512 字节
我需要捕获桌面图像并处理其 RGB 数据,我正在使用 Quartz API 来执行相同的操作, 我面临的问题是内存使用率高, 请引用函数, 在这里编辑,该函数是通过 pThread 调用的;像这样的东
我的 Android 应用程序中有一个 MapActivity,它使用 osmdroid(Open Street Map for Android 库)显示 map 。 当我在此 MapActivity
我在 fragment 中使用 AdMob。有时我会看到以下堆栈 10-23 14:27:38.916: E/ActivityThread(21250): Activity com.applegrew
我正在使用以下方式访问我的 API key ;这似乎是 recommended way ;但是当我将我的应用程序上传到 Play 管理中心时,运行预发布报告时出现严重错误。它说“泄漏的 GCP API
一家 3rd 方安全咨询公司在我们的 Angular SPA/ASP.NET WebAPI 应用程序中发现了 区域下的风险。信息公开 ,我们被告知要解决。 风险是由于 Angular 应用程序的性质,
在 Android 中,当读取 MIFARE Classic 卡时,使用 MifareClassic.authenticateSectorWithKeyA(或 authenticateSectorWi
加载谷歌地图时在分析器中获取泄漏。我根据谷歌的示例代码创建了一个非常简单的 View Controller ,我发现我在加载 map 时遇到了泄漏。我相信泄漏是在 SDK 本身。有没有人遇到过这个问题
我是一名优秀的程序员,十分优秀!