swift3

Swift 3 Core Data - NSExpression forFunction: “sum:” throws error (“could not cast dictionary to Day”)

感情迁移 提交于 2019-12-21 02:44:07
问题 I have days and tasks. A day has many tasks. Every task has an attribute called "points" and I want to sum all points of the tasks of the current day. I used the code below (found in the book Core Data by tutorials, swift 2 version) and tried to modify it for swift 3 (I also added a predicate, but that's not important). But when I run this code, I get this error: Could not cast value of type 'NSKnownKeysDictionary1' (0x10d02d328) to 'MyProject.Day' What am I doing wrong? // sum current day's

How does URLSessionTask run

只谈情不闲聊 提交于 2019-12-21 02:32:11
问题 Say I have created an instance of URLSessionTask : let task = URLSession.shared.dataTask(with: url) { (data, response, error) in print (\(Thread.current)) } // I start the task by task.resume() I want to understand whether the URLSessionTask instance is running on main thread by default or in background thread. So, I print the Thread.current . When I run my code, it print out: <NSThread: 0x170273980>{number = 4, name = (null)} My questions are: which thread the URLSessionTask is running by

Swift 3 - Image view with Tap Gesture

我只是一个虾纸丫 提交于 2019-12-21 02:29:10
问题 Im having issues with using a tap gesture that I have put on an image view. The image is currently stored in the Assets as 'ActionLiked' and I have set the image view to this image. It is then rendered into a table view which is dynamic based on JSON (so it repeats for each item I put into a JSON array). I added the tap gesture to print out 'TAPPED' each time I click on it however, it seems to not be working all the time - 7 items currently in the table, the tap gesture will work on 1 then

How to get memory usage of my application and system in swift by programatically

醉酒当歌 提交于 2019-12-21 01:48:29
问题 How to get memory usage of my application and system in swift by programatically? not only system, but also my application 回答1: For Swift 3.0 you can use the following function. func report_memory() { var taskInfo = mach_task_basic_info() var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size)/4 let kerr: kern_return_t = withUnsafeMutablePointer(to: &taskInfo) { $0.withMemoryRebound(to: integer_t.self, capacity: 1) { task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC

Swift 3 and Xcode8 - Ambiguous use of init

别来无恙 提交于 2019-12-20 20:26:11
问题 Before I installed Xcode 8 and converted project to Swift 3, the following line was fine. Now after conversion it looks like this: let valueData:Data = Data(bytes: UnsafePointer<UInt8>(&intVal), count: sizeof(NSInteger)) it shows the error Ambiguous use of 'init' what is wrong with it in Swift 3? How to fix it? 回答1: The easiest way to create Data from a simple value is to go via UnsafeBufferPointer , then you don't need any explicit pointer conversion or size calculation: var intVal = 1000

To play Youtube Video in iOS app

泪湿孤枕 提交于 2019-12-20 20:01:11
问题 I want to play youtube videos in my iOS App. I searched for that but the only solution I found is to embed youtube videos in the iOS app, in which video plays in webview, so in that, we can scroll and also play other videos which are in suggestion. I don't want to play video in webview, I want to play video just like it plays in player and user cannot scroll it. Is there any solution for that in Swift and also I don't want to use libraries which are against terms and condition of Youtube 回答1:

Swift return data from URLSession

风格不统一 提交于 2019-12-20 19:42:49
问题 I cannot return data from my HTTPrequest and I can't get completion handlers to work either. So please assist me in my quest to solve this issue: public static func createRequest(qMes: message, location: String, method: String) -> String{ let requestURL = URL(string: location) var request = URLRequest(url: requestURL!) request.httpMethod = method request.httpBody = qMes.toString().data(using: .utf8) let requestTask = URLSession.shared.dataTask(with: request) { (data: Data?, response:

Swift 3 changes for getBytes method

♀尐吖头ヾ 提交于 2019-12-20 18:58:10
问题 I have tried to run the below code in swift 3 var values = [UInt8](count:data!.length, repeatedValue:0) data!.getBytes(&values, length:data!.length) where data is 'Data' datatype (NSData is change to 'Data' as per swift 3 guidelines) I am not able to run the above code in Swift 3. Compiler gives error that "Argument Repeated value must precede argument". The same line of code was working in Swift 2.2 What will be the solution ? 回答1: For Swift3 just use following: let array = [UInt8]

UnsafePointer<UInt8> initializer in Swift 3

 ̄綄美尐妖づ 提交于 2019-12-20 17:50:15
问题 I have a receipt validation class that is deprecated since Swift 3 has released. I fixed some issues, but I still have many ... Here is the GitHub source code I used : https://gist.github.com/baileysh9/4386ea92b047d97c7285#file-parsing_productids-swift and https://gist.github.com/baileysh9/eddcba49d544635b3cf5 First Error : var p = UnsafePointer<UInt8>(data.bytes) Compiler throws : Cannot invoke initializer for type UnsafePointer(UInt8) with an argument list of type UnsafeRawPointer Second

What is the Swift 3 equivalent of NSURL.URLByAppendingPathComponent()?

拜拜、爱过 提交于 2019-12-20 16:31:31
问题 I'm following a basic tutorial on building a simple iOS app in Swift. It is written in Swift 2.x, and I work with XCode 8 Beta and Swift 3. The tutorial suggests using NSFileManager to find a data directory. Class names have changed, so the auto-fixed Swift 3 looks like this: static let DocumentsDirectory = FileManager().urlsForDirectory(.documentDirectory, inDomains:.userDomainMask).first! static let ArchiveURL = DocumentsDirectory.URLByAppendingPathComponent("meals") However, Xcode now