文件说
注意
Swift类不从通用基类继承.您定义的类而不指定超类会自动成为您构建的基类."
摘录自:Apple Inc." Swift编程语言."iBooks.
这对我来说没什么意义.Objective-C有一个普遍的基类是有原因的,同样的理由应该适用于Swift,是吗?NSObject
管理保留/释放语义,对默认的实现isEqual:
,hash
和description
.所有这些功能也可以在Swift中使用.
(Objective-C和Swift使用相同的运行时...)
那么,那是什么呢?没有定义超类的Swift类是否NSObject
构成了引擎盖下的正确根类?或者是每个新的根类重复的默认对象行为?或者他们创建了另一个Swift-baseclass?实现retain
和release
非常复杂,因为它需要同时考虑多线程和弱引用.
在Swift中是否存在通用基类(尽管文档中有说明)?这将非常方便,因为在Objective-C中,我可以编写扩展,让我将方法调用合并到主runloop,就像[obj.eventually updateCounter]
可以读作" -updateCounter
下次主runloop控制时调用.如果,同时,我再次调用这个方法,它应该只被调用一次.使用这个扩展可以实现-[UIView setNeedsDisplay]
,[self.eventually display];
如果没有通用基类(或者它是,谁知道?),这在Swift中不再可能.
如果我有一个UIFont
对象,是否可以将其转换为粗体?我不知道字体名称,我只有一个UIFont对象.我想要的是一个像这样的功能
UIFont *boldFontFromFont(UIFont *input)
{
return [input derivedFontWithFontWeight:UIFontWeightBold];
}
Run Code Online (Sandbox Code Playgroud)
如何更改代码以使其正常工作.(上面的代码不起作用,我只是说明了这一点.)
提前致谢.
我想将以下代码转换为Swift:
leftConstraint = [NSLayoutConstraint constraintWithItem:self.contentView
attribute:NSLayoutAttributeLeft
relatedBy:0
toItem:self.view
attribute:NSLayoutAttributeLeft
multiplier:1.0
constant:0];
[self.view addConstraint:leftConstraint];
Run Code Online (Sandbox Code Playgroud)
有人能给我新的语法在Swift中做到吗?
我在使用REPL(= Read-Eval-Print-Loop)时在Swift语言中遇到了一些奇怪的行为,其中似乎有两种不同类型的nil
值在运行时具有不同的行为:
为此,我定义了一个函数g
:
func g(x:String!) {
println("start!");
println((x == "foo") ? "foo" : "not");
}
Run Code Online (Sandbox Code Playgroud)
然后我定义了两个变量:
var x:String
var y:String!
Run Code Online (Sandbox Code Playgroud)
当我打电话时g(x)
,它就像Objective-C一样:
start!
not
Run Code Online (Sandbox Code Playgroud)
当我打电话时g(y)
,我收到一个错误:
start!
fatal error: Can't unwrap Optional.None
Execution interrupted. Enter Swift code to recover and continue.
Enter LLDB commands to investigate (type :help for assistance.)
Run Code Online (Sandbox Code Playgroud)
请注意,此错误在运行时被捕获!该功能已经开始.你可以看到这个因为"开始!" 输出中的字符串.
看来,在第一种情况下,函数的值为nil,并附有"这不是零值"的注释.在第二种情况下,似乎得到一个零,附加说明"这个值是零,请尖叫,不要只使用它."
为什么我在第一种情况下没有出错,在第二种情况下出错?不应该是g(x)和g(y)的行为相同吗?
这是预期的行为吗?我错过了什么吗?这是Swift中的错误吗?规范中的错误?或者实施中的错误?或者只是REPL中的一个错误?是不是不可能访问未初始化的变量?
整个会议记录,供参考......
$ /Applications/Xcode6-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
Welcome to Swift! Type :help for assistance.
1> func g(x:String!) {println("start!"); println((x=="foo") ? …
Run Code Online (Sandbox Code Playgroud) 仅出于教育目的,我想向现有的 iPhone 应用程序添加一个功能,该应用程序是用 ARM 程序集编写的。我一般不需要关于 ARM 汇编的教程,因为我已经阅读了太多的教程。我只是不知道如何实际运行代码!
我想做的是:
没用的.h:
void useless();
Run Code Online (Sandbox Code Playgroud)
没用的.s:
useless:
bx lr
Run Code Online (Sandbox Code Playgroud)
如果这也适用于模拟器,那就没问题了……在模拟器上,.s 文件将无法编译,所以我应该执行以下操作:
没用的.s:
#if I_AM_ARM
useless:
bx lr
#endif
Run Code Online (Sandbox Code Playgroud)
没用的.c:
#if !I_AM_ARM
void useless()
{
}
#endif
Run Code Online (Sandbox Code Playgroud)
我知道我使用的语法已损坏,但我该如何正确编写?(仅仅因为我想尝试一些内联汇编而在模拟器上破坏应用程序是没有选择的......)
第二个最好的选择是使用内联汇编,但我更喜欢非内联汇编。
谢谢!
编辑:我想学习ARM汇编,所以我想找到一种编译ARM汇编代码和EXECUTE ARM汇编代码的方法。
iOS IAP文档指出,如果要测试商店,则应该从设置应用程序中的itunes帐户注销.在进行应用内购买时,系统会要求您输入用户名和密码.但是,在应用程序内部,我不知道用户是否已登录或者他是否正在使用沙盒帐户.
事实上,我并不关心它是否是一个沙箱交易,但是:在服务器上我必须通过URL https://buy.itunes.apple.com/verifyReceipt验证真实收据,并通过URL https验证沙箱收据://sandbox.itunes.apple.com/verifyReceipt.如何在服务器上知道收据是沙箱收据还是真实收据?
或者应用程序如何知道收据是真实的还是沙箱,因为将应用程序中的其他参数传递给我的服务器没有问题.
什么行不通:您不能只说应用程序的DEBUG版本使用沙箱环境,应用程序的ADHOC或RELEASE版本使用生产环境.使用ADHOC版本,完全可以使用iTunes测试帐户进行应用内购买.
在unsafeDupablePerformIO
(来源,文档)的定义中,我找到了对唯一的引用realWorld#
.所以我想知道所有这些以a结尾的函数或值是在哪里#
定义的?我已经找到了GHC primops课程:它们包括seq#
,catch#
,retry#
,和所有其他类型的有趣功能.它还包含一种RealWorld
类型.但是这些初学者似乎并没有包含实际价值realWorld#
.
{-# NOINLINE unsafeDupablePerformIO #-}
-- See Note [unsafeDupablePerformIO is NOINLINE]
unsafeDupablePerformIO :: IO a -> a
unsafeDupablePerformIO (IO m) = lazy (case m realWorld# of (# _, r #) -> r)
-- See Note [unsafeDupablePerformIO has a lazy RHS]
Run Code Online (Sandbox Code Playgroud)
(请不要混合RealWorld
和realWorld#
:第一个是类型,第二个是值.)
GHC还有其他未记载的初学者吗?或者也许所有这些都记录在某处,我还没有找到它?我可以在GHC源中查看这些内容的实现情况吗?
我有一个带有本地化字符串文件的应用程序。 这是我的本地化字符串文件的样子。我还有一个带有包含附件类型(复选标记)的单元格的 tableView。我想让用户在更改 tableView 中的检查时更改我的应用程序的语言。以下是我在应用程序设置的 tableView 中的代码:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.setValues(value: languageArray[indexPath.row]) // Upload new language settings to the server
// Here I want to set the language of my app to the selected value: languageArray[indexPath.row]
// For example, languageArray[indexPath.row] is "en"
var array = tableView.visibleCells
for i in 0 ..< array.count {
let cells: UITableViewCell = array[i]
cells.accessoryType = .none
}
let cell:UITableViewCell! = tableView.cellForRow(at: indexPath)
cell.accessoryType = .checkmark
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
谁能告诉我为什么我们使用guard let self = self
?
我在阅读GCD时已经看过这段代码,我无法弄清楚那条特定的线路是做什么的.
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
guard let self = self else {
return
}
// ...
}
Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string txt="";
ifstream file;
file.open ("ernio.txt", ios::in);
if (file.is_open()) {
while (getline(file, txt)) {
cout << txt << endl;
}
}
else
cout << "example" << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它打印example
而不是从文件中逐行读取.我究竟做错了什么?!?(该文件与main.cpp完全相同)我们甚至尝试过:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string txt="";
ifstream file("ernio.txt");
if (file.is_open()) {
while (getline(file, txt)) {
cout << txt << endl;
}
}
else
cout << "example" << …
Run Code Online (Sandbox Code Playgroud)