Does anybody know of any sample code to create a PDF file using Xcode 9? I have found some great samples on the web, but they were from earlier versions, and I could not migrate the code properly. A simple downloadable sample project that shows how to generate two pages of PDF output with page numbering, a few images, some xCode generated graphics (a simple line would do), and some text would be fantastic. Thank you.
Sample code to create PDF in Xcode 9
What was the problem with code migration.
Can you show examples that you cannot convert ?
Thank you for your reply. I really appreciate it. Sorry I took so long. I just learend how to find all my posts in this forum.
The sample code I was referring to is quite large. Maybe too large for this forum. I have resolved this issue. Coming from C# I am learning that there are some significant diffrences between C# and Swift. When I wrote this post there were several layered issues.
Part of my challenge is that I am an mechanical engineer who learned to program in Jave and Visual Basic. I then migrated to C#.
I am finding learning Swift to be challenging. From what I see online, I am apparently not alone.
A challenge in learning Swift is that if a person gets a code sample, that code will probably not work in Swift 4, even if it is only a year or two old. This has more than tripled the time required for me to learn the language. I will get sample code and it won't compile in Swift 4. So then I try to compile it in a previous version of Swift. Then may or may not run in that earlier version. When I finally get it running in the earlier version and try it in Swift 4, there are other issues. The rapid changes in Swift have, for me, made learning the language a time consuming and challenging experience.
I do not want to start learning Swift from scratch. If I could get a library of sample code that works for Swift 4 it would be helpful. But that doesn't appear to be available. For example, I could not figure out how to get RBG colors into my PDF file. Swift apparently does not come with a way of doing this with a one line command. An internet search revealed no one line way of doing this. I eventually learned to create an extension file and, in that extension file, create a function like this:
NSColor+PDFGenerator.swift
import Foundation
import Cocoa
extension NSColor {
/
static var rgbRedColor: NSColor {
return NSColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0)
}
}
Then I had a one line function call in my drawing code to set the color for the stroke:
AppDelegate.swift
NSColor.rgbRedColor.set()
But, I could find no online resource that showed how to do this. What would have taken minutes in Java or C# ended up taking hours in Swift.
I guess what I am looking for is a way to learn how to create swift results in Swift. Rather than looking for clean code, I want to learn how to get things working quickly. Some of the things I am looking for working sample code in Swift 4 (or the most current version , which may be Swift 5 in a month or two) on the following types of things:
- A way to view the PDF results in Swift before printing them to a PDF file (right now the only thing that seems viable is to actually print the PDF file and then view the file in Swift).
- A one line way to allow the user to choose to over write and existing file when saving from Swift.
- A program to get system sounds on my Mac into a *,.wav file without haveing to rely on third party programs like Soundflower (which doesn't work anymore apparently). I discovered some great sample code but it is for an earlier version, so that means hours of trying to migrate it to Swift 4?
https://github.com/AlesTsurko/LearningCoreAudioWithSwift2.0
- A procedure to take the USDA nutritional database flat files and put them into Core Data.
- A way to run queries on the USDA nutritional database and present the results in a Swift table.
Etc.
Right now I have resigned myself to believing that this cannot be done efficiently in Swift. So, I will take the time that I think I could do any of these things in C#, and multiply it by 5 or 10 in Swift. I am thankful for this resource and, if I continue in Swift, I wil learn to use it appropriately and effectively.
You're going to just need to get a book/tutorial on swift coding. If you know C#, you should pick it up quickly. Your color example is quite contrived. Instead of what you did, you could have just said: NSColor.red.set()
If you really wanted to put a custom color, then yes an extension is "cleaner" as it's reusable for that specific color, but you could have still just said:
NSColor(calibratedRed: 1, green: 0, blue: 0, alpha: 1).set()
Thank you for the advice and code. Do you know of a good book that is written for Swift 4 and has an emphasis on graphics? The pace of changes makes Swift more challenging. Java made a lot of changes but was gentle with deprecated code, depracated code generally worked across several updates. It looks like Swift hard deprecates, and code written for a previous version will simply not run, resulting in lost time. Many of the graphics reference books for Swift are written for previous versions and the code will not work in Swift 4. The Apple documentation of many of the graphics concepts is not written for Swift 4, much of it contains semi colons in the code. I would love to get a book that shows me how to get results in graphical programming with examples that work in Swift 4. It looks like my timing is bad. This is not a good time to learn graphical programming in Swift 4? Please let me konw if you have any suggestions. Right now Visual Studio for Mac looks like the best way forward for me. As long as it actually works on a Mac, I know I can get great results with it quickly. Thanks again.
raywenderlich.com is one of the best sites to go to. They constantly update their stuff.
> The pace of changes makes Swift more challenging.
That pace also means by the time you get your hands on a book, it's outdated, which means you'll spend all your time either begging for support on old/beta code, and/or only learning how to update code you shouldn't have used in the first place.
Gargoyle has a good tip...I'd follow it if I were you.
Thank you. I will work through thoseraywenderlich.com tutorials.
I also wondered if I should just try going directly to Objective C. Hoping that it might be less of a moving target, I started working through the tutorials that are on the developer.apple.com website. It is still troublesome. The Apple website code will not work as is.
For example, I could not get this code to work. It appears that Apple has hard deprecated many parts of Objective C, and has not updated its documentation to work.
I therefore conclude that, at this time, Swift is not a language that is good for one person to transtition to and create good unique rapid developement graphics. It might be great for teams of experts who each devote time to keeping up with their particular area of expertise. I will adapt accordingly. Thanks again.
Opinions obviously vary, but I think you'd be an idiot to spend any time on Objective-C. I've worked multiple very large projects that are 100% Swift. The language is very mature at this point.
Apple's documentation samples are notoriously bad. Even if you did that tutorial in Objective-C it might not even work. They never kept anything up to date.
Again, I had hoped that I was wrong, and just missing something. I will lower my expectations and increase the time estimated to get anything done accordingly. It is what it is. Thank you again.