在Python和Ruby中,带符号的整数除法向负无穷大截断,有符号整数模数与第二个操作数具有相同的符号:
>>> (-41) / 3
-14
>>> (-41) % 3
1
Run Code Online (Sandbox Code Playgroud)
但是,在C和Java中,带符号的整数除法截断为0,有符号整数模数与第一个操作数的符号相同:
printf("%d\n", (-41) / 3); /* prints "-13" */
printf("%d\n", (-41) % 3); /* prints "-2" */
Run Code Online (Sandbox Code Playgroud)
在C和Python中执行相同类型的除法和模数的最简单,最有效的方法是什么?
Objective-C 在XCode 9+/LLVM 5+中有一个@available表达式,它允许您将代码块保护到至少某个操作系统版本,这样如果您使用仅在该范围内可用的API,它就不会发出无人看守的可用性警告操作系统版本
问题在于,这种可用性保护是它只有在它是条件的唯一表达时才有效if.如果您在任何其他上下文中使用它,您会收到警告:
@available does not guard availability here; use if (@available) instead
Run Code Online (Sandbox Code Playgroud)
因此,例如,如果您尝试将可用性检查与以下其他条件进行对比,则它不起作用if:
if (@available(iOS 11.0, *) && some_condition) {
// code to run when on iOS 11+ and some_condition is true
} else {
// code to run when on older iOS or some_condition is false
}
Run Code Online (Sandbox Code Playgroud)
任何在if块内或在块内使用iOS 11 API的代码some_condition都会产生无人看守的可用性警告,即使可以保证只有在iOS 11+上才能访问这些代码.
我可以把它变成两个嵌套if的,但是else代码必须重复,这很糟糕(特别是如果它有很多代码):
if (@available(iOS 11.0, *)) {
if (some_condition) {
// …Run Code Online (Sandbox Code Playgroud) 似乎strtol()并且strtod()有效地允许(并强制)你在字符串中抛弃constness:
#include <stdlib.h>
#include <stdio.h>
int main() {
const char *foo = "Hello, world!";
char *bar;
strtol(foo, &bar, 10); // or strtod(foo, &bar);
printf("%d\n", foo == bar); // prints "1"! they're equal
*bar = 'X'; // segmentation fault
return 0;
}
Run Code Online (Sandbox Code Playgroud)
上面,我自己没有演出.然而,strtol()基本上把我const char *变成了一个char *对我来说,没有任何警告或任何东西.(事实上,它不会让你输入bar的const char *,所以迫使类型的不安全的变化.)是不是真的很危险?
以下代码导致行上的EXC_BAD_ACCESS delegate?.thing():
@class_protocol protocol Fooable {
func foo()
}
class Bar : Fooable {
func foo() {
}
}
weak var delegate: Fooable?
let bar = Bar()
delegate = bar
delegate?.foo()
Run Code Online (Sandbox Code Playgroud)
但一切似乎都适合我.为了变量weak,它必须具有可选类型.所以变量delegate是可选的.弱变量的类型也必须是类类型,因此我将协议作为类协议.因为我使用可选链接,所以我希望它可以1)成为nil,什么也不做,或2)不成为nil,并调用方法,这应该成功.然而,它崩溃了.
是否可选链接不是原子的,并且不保留表达式,并且对象在检查nil和后续调用之间以某种方式被解除分配?
有趣的是,如果你消除变量bar并直接分配它delegate = Bar(),崩溃就会消失.这实在令人困惑,因为将表达式赋给变量然后分配变量并直接指定表达式通常应该表现相同.
我想只有在当前设备的iOS版本低于特定版本运行一段代码,作为指定在这里.Apple给出的代码示例如下所示:
if (@available(iOS 10.0, *)) {
// iOS 10.0 and above
} else {
// below 10.0
}
Run Code Online (Sandbox Code Playgroud)
但是,有些情况下,只有当前iOS版本低于特定版本时,才会运行代码.我假设以下代码将起作用:
if (!@available(iOS 10.0, *)) {
// below 10.0
}
Run Code Online (Sandbox Code Playgroud)
但似乎这不起作用,我从Xcode收到以下警告:
@available does not guard availability here; use if (@available) instead
Run Code Online (Sandbox Code Playgroud)
这是添加了我所看到的诊断的LLVM提交.
该问题有两种可能的后备:
if-else变体而不向if块添加任何代码(不是很优雅).-[NSProcessInfo isOperatingSystemAtLeastVersion:].还有另一种使用@available我想要的方法吗?
更新:我尝试编写它而不会使它变弱,并且似乎没有泄漏.所以也许问题不再是必要的.
在Objective-C ARC中,当你想让一个闭包能够在闭包内部使用它时,该块不能捕获对它自身的强引用,或者它将是一个保留循环,所以你可以使闭包捕获一个对自身的弱引用,如下:
// This is a simplified example, but there are real uses of recursive closures
int (^fib)(int);
__block __weak int (^weak_fib)(int);
weak_fib = fib = ^(int n) {
if (n < 2)
return n;
else
return weak_fib(n-1) + weak_fib(n-2);
};
Run Code Online (Sandbox Code Playgroud)
我试图将其转换为Swift:
var fib: (Int -> Int)?
fib = { [weak fib] (n: Int) in // 'weak' cannot be applied to non-class type 'Int -> Int'
if n < 2 {
return n
} else {
return fib!(n-1) …Run Code Online (Sandbox Code Playgroud) closures weak-references objective-c-blocks retain-cycle swift
在Java中我可以通过使用a Iterator然后使用.remove()迭代器的方法来删除迭代器返回的最后一个元素,如下所示:
import java.util.*;
public class ConcurrentMod {
public static void main(String[] args) {
List<String> colors = new ArrayList<String>(Arrays.asList("red", "green", "blue", "purple"));
for (Iterator<String> it = colors.iterator(); it.hasNext(); ) {
String color = it.next();
System.out.println(color);
if (color.equals("green"))
it.remove();
}
System.out.println("At the end, colors = " + colors);
}
}
/* Outputs:
red
green
blue
purple
At the end, colors = [red, blue, purple]
*/
Run Code Online (Sandbox Code Playgroud)
我将如何在Python中执行此操作?我在for循环中迭代它时无法修改列表,因为它会导致跳过东西(参见此处).并且似乎没有相当于IteratorJava 的接口.
UINavigationItem titleView属性的文档说:"如果leftBarButtonItem不是nil,则忽略此属性."
但是,我在测试中设置了两个titleView和leftBarButtonItem属性,它们看起来都很好.我已经在我的Xcode中测试了所有模拟器:4.3,5.0和5.1.有没有人知道文档是错误的,还是在一些我无法测试的旧版本(例如4.2,3.x)上是否正确?
iphone uinavigationbar uikit uinavigationcontroller uinavigationitem
-setKeepAliveTimeout:handler:不推荐使用的iOS9状态的当前API更改.
到目前为止,这是iOS上的VoIP SIP应用程序可以维持其与SIP服务器的注册的唯一方式.
这种技术被LinPhone等其他应用程序使用.
有没有人对Apple提出的替代方案有所了解?或者,从(后)iOS9开始,SIP是否会瘫痪?
请参阅:https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/setKeepAliveTimeout : handler:
我正在使用Xcode 7.3,并且我得到了一个"Ambiguous expansion of macro"警告,对于一个在Foundation中定义的宏,但我在我的前缀文件中未定义和重新定义.我启用了模块.
重现:
使用以下前缀文件:
#import <Foundation/Foundation.h>
#undef assert
#define assert(e) NSLog(@"hi") // implementation is not important
Run Code Online (Sandbox Code Playgroud)使用以下主要源文件:
int main() {
assert(42);
return 0;
}
Run Code Online (Sandbox Code Playgroud)然后在Xcode中构建.
禁用模块时不会发生此警告.
ios ×3
availability ×2
c ×2
objective-c ×2
swift ×2
xcode9 ×2
c++ ×1
c-strings ×1
clang ×1
closures ×1
const ×1
const-char ×1
division ×1
ios11 ×1
ios9 ×1
iphone ×1
iterator ×1
java ×1
keep-alive ×1
list ×1
loops ×1
macros ×1
modulo ×1
python ×1
registration ×1
retain-cycle ×1
sip ×1
std ×1
uikit ×1
voip ×1
xcode ×1