基本上我想要的是获得仅以小时为单位表示的时间间隔的值,而不是将其四舍五入到整小时(使用NSDateComponentsFormatter来使其正确格式化和本地化).我不知道我是否误解了NSDateComponentsFormatter.allowsFractionalUnits的使用,但我无法让格式化程序给我一个十进制值.任何人都可以帮助我发现我的错误或告诉我我以何种方式误解了这个?
从Apple docs about allowFractionalUnits属性:
当使用可用单位无法准确表示值时,可以使用小数单位.例如,如果不允许分钟,则值"1h 30m"可以格式化为"1.5h".
Swift示例代码:
let formatter = NSDateComponentsFormatter()
formatter.unitsStyle = .Abbreviated
formatter.allowedUnits = .Hour
formatter.allowsFractionalUnits = true
let onePointFiveHoursInSeconds = NSTimeInterval(1.5 * 60.0 * 60.0)
print(formatter.stringFromTimeInterval(onePointFiveHoursInSeconds)!)
//"1h" instead of expected "1.5h"
Run Code Online (Sandbox Code Playgroud)
Objective-C代码中的相同示例:
NSDateComponentsFormatter *formatter = [[NSDateComponentsFormatter alloc] init];
formatter.unitsStyle = NSDateComponentsFormatterUnitsStyleAbbreviated;
formatter.allowedUnits = NSCalendarUnitHour;
formatter.allowsFractionalUnits = YES;
NSTimeInterval onePointFiveHoursInSeconds = 1.5 * 60.0 * 60.0;
NSLog(@"%@", [formatter stringFromTimeInterval:onePointFiveHoursInSeconds]);
//"1h" instead of expected "1.5h"
Run Code Online (Sandbox Code Playgroud)
更新:我已向Apple报告了有关此问题的错误(rdar:// 22660145).
题
import Swift和之间有什么区别import Foundation?
直到我读这条评论被马丁- [R ,我甚至不知道有一个import Swift.
读
我找不到文档,谷歌搜索没有太多.
我尝试了什么
测试它显示import Swift没有给出任何编译错误,但这并没有真正回答我的问题.
如果我猜测,我会说您导入Swift for Swift项目,并导入Foundation for Objective-C项目,或者可能导入使用Objective-C类的Swift项目(如NSString).
在Playground中测试:
import Foundation
import Swift
var str = "Hello, playground"
let str2: NSString = "hello"
let str3: String = "hello"
Run Code Online (Sandbox Code Playgroud)
注释掉import Swift没有错误,str属于String类型.但是,注释掉import Foundation会给出"未声明的类型"错误NSString.
我的问题重新审视
我很高兴放弃基金会并且只使用Swift.所以,除非我特别需要使用旧的Objective-C类之一,否则我一直只能导入Swift?
我使用Xcode 8.0 beta来测试我的应用程序,但在将我的代码2.2转换为3.0之后,出现了许多未解决的错误.在演示下面,Xcode8.0出现错误消息
1.在"如果"条件之后预期"{"
if annotation.isKindof: (MKUserLocation) {
return nil
}
Run Code Online (Sandbox Code Playgroud)
第二个错误是地图套件...我使用iskind(of:MKUserLocation)仍然无法解决,它会出现另一个错误消息
2.Braced语句块是一个未使用的闭包
if annotation.isKindof: (MKUserLocation) {
return nil
}
Run Code Online (Sandbox Code Playgroud)
3.在通话中缺少参数标签'of:'
if annotation.isKind(of: MKUserLocation) {
return nil
}
Run Code Online (Sandbox Code Playgroud) 假设我有NSSet一个字符串.如果我发送containsObject:另一个字符串,它是一个不同的指针,但完全相同的字符串值,将返回YES?
此外,它涉及到同样的故事removeObject:吗?即,具有不同指针但具有相同字符串值的不同对象将导致删除?
在Objective-C中,我的程序打开一个窗口并显示一个表.我希望突出显示表的指定行.
我该怎么做呢?
我似乎需要代码
[myTableView selectRowIndexes:(NSIndexSet *) byExtendingSelection:(BOOL)];
Run Code Online (Sandbox Code Playgroud)
我查看了开发人员文档,并发现BOOL应该是NO.
通过查看NSIndexSet文档,我无法弄清楚正确的语法应该是什么.
我有一个NSDictionary,其中每个键指向一个数组.我后来想要将所有值合并到一个数组中.有没有办法使用API来做一些比说更有效的事情:
NSArray *anArray = [someDictionary allValues];
NSMutableArray *newArray = [NSMutableArray array];
start outter loop on anArray
start inner loop on objects in anArray
add objectAtIndex to newArray
Run Code Online (Sandbox Code Playgroud) 在iPhone开发中,速度至关重要.有没有人知道使用CoreFoundation类型(如CFMutableDictionaryRef)与Foundation类型(其对应的NSMutableDictionary)之间是否存在速度差异.
我认为操纵CF类型会更快,因为它不必抛出ObjC运行时消息,这是一个毫无根据的假设,有没有人真正看过这个?
-詹姆士
使用自定义 DataTransformers 时,Xcode 13 和 iOS 15 开始发出关于 MissingAllowedClasses 的警告。关于自定义 DataTransformers 的文档非常少,所以我想我应该在这里发布一个问题并回答它。
[general] *** -[NSKeyedUnarchiver _warnAboutPlistType:missingInAllowedClasses:] allowed unarchiving safe plist type ''NSString' (0x1dc9a7660) [/System/Library/Frameworks/Foundation.framework]', even though it was not explicitly included in the client allowed classes set: '{(
"'NSArray' (0x1dc99c838) [/System/Library/Frameworks/CoreFoundation.framework]"
)}'. This will be disallowed in the future.
Run Code Online (Sandbox Code Playgroud)
请注意,在警告消息中,它指定了“NSArray”,并且缺少类型“NSString”。
所以我正在开始一个新的NSThread,我希望以后可以通过调用来使用它performSelector:onThread:....根据我的理解,调用方法将该调用添加到该线程上的runloop,因此在下一次迭代时,它将弹出所有这些调用,然后调用它们,直到没有任何东西可以调用.所以我需要这种功能,一个空闲的线程准备工作,我可以调用它.我当前的代码如下所示:
- (void)doInitialize
{
mThread = [[NSThread alloc] initWithTarget:self selector:@selector(runThread) object:nil];
[mthread start];
}
- (void)runThread
{
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
// From what I understand from the Google machine is that this call should start the
// runloop on this thread, but it DOESN'T. The thread dies and is un-callable
[[NSRunLoop currentRunLoop] run];
[pool drain];
}
- (void)scheduleSomethingOnThread
{
[self performSelector:@selector(hardWork) onThread:mThread withObject:nil waitUntilDone:NO];
}
Run Code Online (Sandbox Code Playgroud)
但是线程没有保持活动状态,performSelector:onThread没有做任何事情.我该怎么做正确的方法?
找到了这个问题并找不到多少帮助.
"could not build module 'Foundation'"
#import < Foundation/Foundation.h>
Run Code Online (Sandbox Code Playgroud)
不确定是什么问题.谢谢你的任何建议.
foundation ×10
cocoa ×5
objective-c ×5
ios ×4
swift ×4
cloudkit ×1
collections ×1
import ×1
nsindexset ×1
nsrunloop ×1
nsset ×1
nsthread ×1
swift3 ×1
xcode ×1