Tag: swift2

在Swift中parsingCSV文件

当应用程序启动时,我需要将数据预加载到我的tableView中。 所以我通过parsing.csv文件来使用核心数据。 我正在为此目的而学习本教程 。 这是我的parseCSV函数 func parseCSV (contentsOfURL: NSURL, encoding: NSStringEncoding, error: NSErrorPointer) -> [(stationName:String, stationType:String, stationLineType: String, stationLatitude: String, stationLongitude: String)]? { // Load the CSV file and parse it let delimiter = "," var stations:[(stationName:String, stationType:String, stationLineType: String, stationLatitude: String, stationLongitude: String)]? let content = String(contentsOfURL: contentsOfURL, encoding: encoding, error: error) stations = […]

如何使用GPUImageHarrisCornerDetectionFilter获得转angular

我正在尝试使用GPUImageHarrisCornerDetectionFilter从静止图像中获取angular点。 我已经看了这个项目中的代码示例,我已经看过这个文档,并且看过这个post,这个post大概是这样的: 在现有的UIImage上,GPUImage Harris Corner Detection给出了一个黑屏输出 但是我不能使它工作 – 而且我很难理解这是如何处理静止图像的。 我现在所拥有的是: func harrisCorners() -> [CGPoint] { var points = [CGPoint]() let stillImageSource: GPUImagePicture = GPUImagePicture(image: self.image) let filter = GPUImageHarrisCornerDetectionFilter() filter.cornersDetectedBlock = { (cornerArray:UnsafeMutablePointer<GLfloat>, cornersDetected:UInt, frameTime:CMTime) in for index in 0..<Int(cornersDetected) { points.append(CGPoint(x:CGFloat(cornerArray[index * 2]), y:CGFloat(cornerArray[(index * 2) + 1]))) } } filter.forceProcessingAtSize(self.image.size) stillImageSource.addTarget(filter) stillImageSource.processImage() return […]

Swift 2.0:使用Set时无法将MyApp.MyCustomClass值转换为MyAppTests.MyCustomClass

这是一个错误: 无法将typesMyApp.Member (0x1674daf8)的值转换为MyAppTests.Member (0x4c07248)。 你可以很容易地重现一个错误: 安装两个NSManagedObject @objc(Member) class Member: NSManagedObject { @NSManaged var family: Family } @objc(Family) class Family: NSManagedObject { @NSManaged var members: Set<Member> } 在.xcdatamodel也可以.xcdatamodel : 然后在你的TestFile中 : func testA() { let family = Family.MR_createEntityInContext(context) let father = Member.MR_createEntityInContext(context) father.family = family let firstMember = family.members.first XCTAssertEqual(firstMember!, father) }

如何使用Swift 2 + XCode 7 + iOS 9获取和parsingJSON

正在寻找有用的教程与Swift 2和iOS 9的networking,但似乎主题没有在线内容。 我已经观看了WWDC会话使用NSURLSession进行networking连接但是我无法调整iOS9的新API来获取和parsingJSON数据的asynchronous方式。 我曾经尝试过: do { if let url = NSURL(string: "site/api.php?option=fetchOps"), let data = NSData(contentsOfURL: url), let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [[String:AnyObject]] { } } catch let error as NSError { print(error.description) } 请分享你在iOS9和Swift2networking上的经验,任何最佳实践也许是一个Singleton类的networking和parsing可能是一个ORM?

将一个JSON数组反序列化为一个Swift的对象数组

我是新来的Swift,我无法弄清楚如何将一个JSON数组反序列化成一个Swift对象数组。 我可以反序列化一个单一的JSON用户到一个Swift用户对象罚款,但只是不知道如何做一个JSON数组的用户。 这是我的User.swift类: class User { var id: Int var firstName: String? var lastName: String? var email: String var password: String? init (){ id = 0 email = "" } init(user: NSDictionary) { id = (user["id"] as? Int)! email = (user["email"] as? String)! if let firstName = user["first_name"] { self.firstName = firstName as? String } […]

tableView.dequeueReusableCellWithIdentifier()导致应用程序挂起

原来的post 我们最近已经将我们的应用程序转换为Swift 2.0和iOS9。 我看到一个奇怪的问题是调用tableView.dequeueReusableCellWithIdentifier()导致应用程序挂在模拟器中。 代码 func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { //hangs on the line below let headersection: HeaderSectionCell = tableView.dequeueReusableCellWithIdentifier("SectionHeader") as! HeaderSectionCell … return headersection } 标题单元格 class HeaderSectionCell: UITableViewCell { @IBOutlet var labelOne: UITextView! @IBOutlet var labelTwo: UITextView! @IBOutlet var textView: UITextView! } 模拟器CPU使用率在100% 在Xcode暂停之后,它显示了它挂在这个Swift函数上。 以下是iOS正在循环下的一些例程。 最后,我们的Swift调用dequeueReusableCellWithIdentifier() 这个特殊的悬挂实例是从函数tableView(tableView: UITableView, viewForHeaderInSection […]

Swift – “共享应用程序不可用”。 代之以使用视图控制器的解决scheme

我打开一个外部URL在我的Swift应用程序使用行: UIApplication.sharedApplication().openURL(url) 这工作正常,直到我通过CocoaPods添加Realm到我的项目。 在这一点上,它开始给我下面的两个编译错误: 'sharedApplication()'不可用:取而代之,使用基于视图控制器的解决scheme。 “openURL”不可用 这些错误表明该API对于应用程序扩展是不可用的,但是我的代码位于正常的应用程序中,而不是扩展名。 为什么添加Realm导致编译器认为它在App Extension中?

非有限位置的子层

我正在使用Objective-C中编写的第三个CocoaPods库来截取UITextView。 对于iOS 8来说是可以的,但是当我更改iOS 9和Swift 2的语法之后,它会抛出一个错误: 终止应用程序由于未捕获的exception“CALayerInvalidGeometry”,原因:“非有限位置的子层[inf inf]' 这是来自图书馆的代码: – (UIImage *)screenshotForCroppingRect:(CGRect)croppingRect { UIGraphicsBeginImageContextWithOptions(croppingRect.size, NO, [UIScreen mainScreen].scale); // Create a graphics context and translate it the view we want to crop so // that even in grabbing (0,0), that origin point now represents the actual // cropping origin desired: CGContextRef context = UIGraphicsGetCurrentContext(); if (context == […]

我怎样才能在Swift中获取值types或自定义结构的内存地址?

我试图深入了解Swift如何复制值types : 您在代码中看到的行为将始终像发生副本一样。 但是,当绝对必要的时候,Sw​​ift只能在幕后执行一个实际的副本。 为了提高我的理解,我想获得一个值types的内存地址。 我尝试unsafeAddressOf(),但这不适用于结构,它似乎将Swift的标准库types转换为引用types (例如,string转换为NSString)。 我怎样才能得到一个值types的内存地址,像一个Int的实例,或在Swift中的自定义结构?

无法使用AudioFileStreamOpen的AudioFileStream_PacketsProccallback中的AudioConverterFillComplexBuffer将MP3转换成PCM

我有一个AudioFileStream_PacketsProccallback设置在一个AudioFileStreamOpen处理转换audio数据包到PCM使用AudioConverterFillComplexBuffer 。 我遇到的问题是我调用AudioConverterFillComplexBuffer后得到一个-50 OSStatus(paramErr)。 下面是在AudioConverterFillComplexBuffer中使用的参数的一个片段,以及它们是如何制作的: audioConverterRef = AudioConverterRef() // AudioConvertInfo is a struct that contains information // for the converter regarding the number of packets and // which audiobuffer is being allocated convertInfo? = AudioConvertInfo(done: false, numberOfPackets: numberPackets, audioBuffer: buffer, packetDescriptions: packetDescriptions) var framesToDecode: UInt32 = pcmBufferTotalFrameCount! – end var localPcmAudioBuffer = AudioBuffer() localPcmAudioBuffer.mData = […]