草庐IT

range-init

全部标签

ios - PDFKit 的 PDFDocument init(url : URL) does not work with HTTPS

当我尝试使用PDFDocument.init(url:)时,我注意到它适用于http但不适用于httpsURL。有谁知道为什么吗?importPDFKitlethttpURL=URL(string:"http://www.axmag.com/download/pdfurl-guide.pdf")!letdoc1=PDFDocument(url:httpURL)//makesaPDFDocumentlethttpsURL=URL(string:"https://www.gnu.org/s/libmicrohttpd/tutorial.pdf")!letdoc2=PDFDocument(u

swift 3 : Filter a range

在Swift2中,可以像这样过滤范围:letrange:Range=1..在Swift3中,范围似乎已经失去了它的过滤方法。有什么建议吗? 最佳答案 你必须使用可数范围:letrange:CountableRange=1..一个(Closed)Range描述了一个“区间”,不能被枚举,而Countable(Closed)Range是连续值的集合。 关于swift3:Filterarange,我们在StackOverflow上找到一个类似的问题: https:/

swift - swift 3 中 UIView.init 和没有 init 的区别

这个问题在这里已经有了答案:InSwift,what'sthedifferencebetweencallingUINavigationController()vsUINavigationController.init()?(3个答案)关闭5年前。我正在以编程方式添加两个UIView。如下例所示:-letfirstview=UIView(frame:CGRect(x:0,y:self.view.frame.height-63,width:self.view.frame.width,height:80))letsecondview=UIView.init(frame:CGRect(x:0,

swift - 如何为仅在测试中可见的结构创建 init 方法?

我想为我的结构创建一个初始化方法,其中包含一组用于单元测试的默认值。我不希望主模块能够看到这个init方法。我以为我可以简单地在我的测试类的文件中创建我的结构的扩展,但这不起作用:Foo.swift(在应用目标中):publicstructFoo{publicletbar:Intpublicinit(colour:String){self.bar=colour=="Green"?0:1}}FooExtension.swift(在测试目标中):extensionFoo{publicinit(bar:Int=42){self.bar=bar}}请注意主文件中的非默认初始化程序,因此Swif

ios - 来自 Swift Range 的 NSRange?

问题:NSAttributedString在我使用使用Range的SwiftString时采用NSRangelettext="Longparagraphsayingsomethinggoeshere!"lettextRange=text.startIndex..()inif(substring=="saying"){attributedString.addAttribute(NSForegroundColorAttributeName,value:NSColor.redColor(),range:substringRange)}})产生以下错误:error:'Range'isnotco

swift - NSDictionary - 无法调用 "init"

这是代码letdictionary=NSDictionary(objects:[user.username,image],forKeys:["username","image"])我遇到了错误无法使用参数列表类型调用“init”(对象:$T3,forKeys:$T7)更多代码显示如何创建user.username和图像letusers=objectsforuserIdinself.arrayUserIds{letuser=self.getUserFromUserId(userIdasString,arrayUsers:users)self.arrayUserFriends.addObj

swift - 父类(super class)上的通用便利 init() 受限于已初始化对象的子类?

是否可以在Swift3中编写初始化程序,以便将T限制为创建对象的类型?(它定义的类的子类)效果是//NotrealcodeextensionUIView{convenienceinit(style:(T)->Void...)whereT==self.dynamicType{self.init()forsinstyle{s(self)}}}...letv1=UIView(style:funcThatAppliesToUIView)letv2=UILabel(style:funcThatAppliesToUIView,funcThatAppliesToUILabel)

Swift:为什么 init?(length length: Int) NSMutableData 的初始化程序会失败?

内存分配可能会失败,但我认为Swift不会处理这种情况。github上的代码调用一个不可失败的初始化器publicconvenienceinit?(length:Int){letmemory=malloc(length)self.init(bytes:memory,length:length,copy:false){buffer,amountinfree(buffer)}}编辑:代码来自Swift3中的原生SwiftFoundation框架。 最佳答案 来自swift-users列表:“Swift关于内存分配失败的政策是,如果无法处

Swift 子类的 init 必须在调用 super 之前设置所有属性,但是属性需要 super 的 init 先完成

我正在尝试编写两个Swift类,一个是另一个的子类,需要采用父类(superclass)的一个属性并使用它来配置自身。classBaseClass{letsomeValue:Doubleletsize:CGSizeinit(size:CGSize){self.size=sizeself.someValue=size.width/2.0//Doesn'treallymatterhowthisiscalculated}}classSubclass:BaseClass{letsomeNewValue:Doubleoverrideinit(size:CGSize){super.init(siz

ios - Swift UIViewController Custom init() - 错误无法分配给自己

在Objective-C中,我曾经覆盖UIViewController的init方法。我无法在Swift中实现相同的目标:Objective-C代码:-(instancetype)init{self=[superinit];if(self){self=[[UIStoryboardstoryboardWithName:@"Main"bundle:[NSBundlemainBundle]]instantiateViewControllerWithIdentifier:@"ViewController"];}returnself;}如果我尝试在Swift中这样做,我会收到“无法分配给自己”的