1. Trang chủ
  2. » Công Nghệ Thông Tin

iOS 8 swift programming cookbook

946 1.2K 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

[...]... Introduction In order to write apps for iOS, you need to know some of the basics of the Swift programming language that we will use throughout this book Swift is Apple’s new programming language introduced in Xcode 6 and iOS 8 SDK Objects and classes are fundamental in object-oriented programming (OOP) languages such as Swift, Objective-C, Java, C++, and many others All iOS applications essentially use the... structure of an iOS application and how to use views and view controllers to create intuitive applications I also want to teach you a few basics of the Swift programming language—but before we begin, I want to make it absolutely obvious that the goal of this book is not to teach you how to program in Swift Apple has already released a full book more than 500 pages long that teaches you how to use Swift But... resource to learn Swift, I will go over a few basics Defining Constants and Variables in Swift We define constants with the let keyword like so: let integerValue = 10 let stringValue = "Swift" let doubleValue = 10.0 The value that we assign to a constant (or later to a variable) defines its type In the examples I gave, we did not have to define the data type of the constants at all because the Swift compiler... constant: var allStrings = [String]() allStrings.append( "Swift" ) allStrings.append("Objective-C") /* Our array is now [ "Swift" , "Objective-C" */ If you want to access values inside an array, use subscripting with the [] syntax: var allStrings = [String]() allStrings.append( "Swift" ) allStrings.append("Objective-C") println(allStrings[0]) /* Prints out "Swift" */ allStrings.insert("C++", atIndex: 0) println(allStrings[0])... keyword: var myString = "Swi" myString += "ft" /* myString is now "Swift" */ Variables can be mutable or immutable An immutable variable cannot be changed or appended to Mutable variables can be changed Creating and Using Arrays in Swift The [DataType] syntax can create an array This is an example of creating an immutable array: let allStrings = [ "Swift" , "Objective-C"] If you want to create a mutable array,... it was passed to a function that changed the first name Classes can also have inheritance, but structures cannot have inheritance Diving into Operators in Swift There are many valid operators in Swift Here are a few examples: typealias byte = UInt8 @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions... class ViewController: UIViewController { var label: UILabel! override func viewDidLoad() { super.viewDidLoad() label = UILabel(frame: CGRect(x: 20, y: 100, width: 100, height: 23)) label.text = "iOS Programming Cookbook" label.font = UIFont.boldSystemFontOfSize(14) view.addSubview(label) } } Now let’s run our app and see what happens (see Figure 1-6) Figure 1-6 A label that is too small in width to contain... override func viewDidLoad() { super.viewDidLoad() label = UILabel(frame: CGRect(x: 20, y: 100, width: 100, height: 70)) label.numberOfLines = 3 label.lineBreakMode = ByWordWrapping label.text = "iOS Programming Cookbook" label.font = UIFont.boldSystemFontOfSize(14) view.addSubview(label) } } If you run the app now, you will get the desired results (see Figure 1-7) Figure 1-7 A label wrapping its contents... var label: UILabel! override func viewDidLoad() { super.viewDidLoad() label = UILabel(frame: CGRect(x: 20, y: 100, width: 100, height: 23)) label.adjustsFontSizeToFitWidth = true label.text = "iOS Programming Cookbook" label.font = UIFont.boldSystemFontOfSize(14) view.addSubview(label) } } Rich text is a thing of legend! A lot of us programmers have had the requirement to display mixedstyle strings... class or structure you like Just ensure that your operator overloaders are public functions and not defined inside any specific class or structure Declaring and Using Enumerations in Swift Enumerations are very sophisticated in Swift indeed They can be of any given type For instance, they can be strings: enum CarClassification: String{ case Estate = "Estate" case Hatchback = "Hatchback" case Saloon = "Saloon" . author, publisher, and ISBN. For example: iOS 8 Swift Programming Cookbook by Vandad Nahavandipoor (O’Reilly). Copyright 2015 Vandad Nahavandipoor, 9 78- 1-4919- 086 9-3.” If you feel your use of code. for iOS, you need to know some of the basics of the Swift programming language that we will use throughout this book. Swift is Apple’s new programming language introduced in Xcode 6 and iOS 8 SDK

Ngày đăng: 02/08/2015, 18:52

Xem thêm: iOS 8 swift programming cookbook

TỪ KHÓA LIÊN QUAN

Mục lục

    iOS 8 Swift Programming Cookbook

    Organization of This Book

    Conventions Used in This Book

    How to Contact Us

    1.1. Adding Blur Effects to Your Views

    1.2. Presenting Temporary Information on the Screen with Popovers

    1.3. Displaying Images with UIImageView

    1.4. Displaying Static Text with UILabel

    1.5. Adding Buttons to the User Interface with UIButton

    1.6. Displaying Alerts and Action Sheets

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN