CSC/ECE 517 Fall 2014/ch1a 13 va: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
 
(30 intermediate revisions by 2 users not shown)
Line 4: Line 4:
[[File:playgrounds.png|center|Fig 1: Playgrounds with Swift in Xcode v6.0 ]]
[[File:playgrounds.png|center|Fig 1: Playgrounds with Swift in Xcode v6.0 ]]
</div>
</div>
<p>On June 2, 2014 at the [http://en.wikipedia.org/wiki/Apple_Worldwide_Developers_Conference World Wide Developers Conference], Apple announced version 6 of Xcode. Features include Playgrounds live debugging tools, as well as an entirely new programming language called Swift. A key element of the Swift system is its ability to be cleanly debugged and run within the development environment, using a [http://en.wikipedia.org/wiki/Read–eval–print_loop read–eval–print loop (REPL)], giving it interactive properties more in common with scripting systems like Python than traditional systems programming languages. The REPL is further enhanced with the new 'playgrounds' concept; 'playgrounds' are interactive views running within the Xcode environment that respond to code or debugger changes on-the-fly. If the code in question changes over time or with regard to some other ranged input value, the view can be used with the Timeline Assistant to demonstrate the output in an animated fashion.</p>
<p>On June 2, 2014 at the [http://en.wikipedia.org/wiki/Apple_Worldwide_Developers_Conference World Wide Developers Conference], Apple announced version 6 of Xcode. Features include Playgrounds live debugging tools, as well as an entirely new programming language called Swift. A key element of the Swift system is its ability to be cleanly debugged and run within the development environment, using a [http://en.wikipedia.org/wiki/Read–eval–print_loop read–eval–print loop (REPL)]<ref>[http://en.wikipedia.org/wiki/Swift_(programming_language) Swift]</ref>, giving it interactive properties more in common with scripting systems like Python than traditional systems programming languages. The REPL is further enhanced with the new 'playgrounds' concept; 'playgrounds' are interactive views running within the Xcode environment that respond to code or debugger changes on-the-fly. If the code in question changes over time or with regard to some other ranged input value, the view can be used with the Timeline Assistant to demonstrate the output in an animated fashion.</p>


Apple claims that Swift "is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language."<ref name="Topic">[https://docs.google.com/a/ncsu.edu/document/d/1eq3XHiUUBIrEBx-R2Mgolaw7HttSgzPniwDLEF5oVk0/edit# Topic Document]</ref>
Apple claims that Swift "is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language."<ref name="Topic">[https://docs.google.com/a/ncsu.edu/document/d/1eq3XHiUUBIrEBx-R2Mgolaw7HttSgzPniwDLEF5oVk0/edit# Topic Document]</ref>
Line 16: Line 16:


===Xcode===
===Xcode===
Xcode is an [http://en.wikipedia.org/wiki/Integrated_development_environment Integrated Development Environment] (IDE) developed by Apple for developing software for iOS and OS X. The Xcode suite consists of the IDE, Apple’s developer documentation and a built-in interface builder. It is shipped free with Mac OS X 10.3, but is able to develop OS X applications that can run on any version of OS X. It extends and replaces Apple's earlier tool, Project Builder, which was inherited from [http://en.wikipedia.org/wiki/NeXT NeXT]. However, Xcode officially does not work in Mac OS X 10.2.It works hand in hand with Interface Builder (also inherited from NeXT), a graphical tool used to create user interfaces.
Xcode is an [http://en.wikipedia.org/wiki/Integrated_development_environment Integrated Development Environment] (IDE) developed by Apple for developing software for iOS and OS X. The Xcode suite consists of the IDE, Apple’s developer documentation and a built-in interface builder. It is shipped free with Mac OS X 10.3, but is able to develop OS X applications that can run on any version of OS X. It extends and replaces Apple's earlier tool, Project Builder, which was inherited from [http://en.wikipedia.org/wiki/NeXT NeXT]. However, Xcode officially does not work on Mac OS X 10.2.It works hand in hand with Interface Builder (also inherited from NeXT), a graphical tool used to create user interfaces.


Xcode includes GCC, and can compile [http://en.wikipedia.org/wiki/C C], [http://en.wikipedia.org/wiki/C%2B%2B C++], Objective C++, [http://en.wikipedia.org/wiki/java Java], and [http://en.wikipedia.org/wiki/Objective-C Objective C] source code with a variety of programming models, including but not limited to [http://en.wikipedia.org/wiki/Cocoa_(API) Cocoa], Carbon, and Java.
Xcode includes GCC, and can compile [http://en.wikipedia.org/wiki/C C], [http://en.wikipedia.org/wiki/C%2B%2B C++], Objective C++, [http://en.wikipedia.org/wiki/java Java], and [http://en.wikipedia.org/wiki/Objective-C Objective C] source code with a variety of programming models, including but not limited to [http://en.wikipedia.org/wiki/Cocoa_(API) Cocoa], Carbon, and Java.
Line 22: Line 22:


===Swift===
===Swift===
Swift is a new programming language that adopts safe programming patterns and makes programming flexible. Swift combines the best of both C and Objective-C. It integrates both - the procedural features like types and operators as well as the object-oriented features such as classes and protocols.  It adopts readability and dynamic object model from Objective-C and has added new features like blocks, collection literals and modules, fast performance. Swift has many advantages over Objective-C, like being more type-safe, having more natural and concise syntax, no need for header/implementation files. It has simplified memory management with [http://en.wikipedia.org/wiki/Automatic_Reference_Counting Automatic Reference Counting]. Swift is a fast and powerful programming language that is easier and more fun to implement.
Swift<ref>[https://developer.apple.com/swift/ Introduction to Swift]</ref> is a new programming language that adopts safe programming patterns and makes programming flexible. Swift combines the best of both C and Objective-C. It integrates both - the procedural features like types and operators as well as the object-oriented features such as classes and protocols.  It adopts readability and dynamic object model from Objective-C and has added new features like blocks, collection literals and modules, fast performance. Swift has many advantages over Objective-C, like being more type-safe, having more natural and concise syntax, no need for header/implementation files. It has simplified memory management with [http://en.wikipedia.org/wiki/Automatic_Reference_Counting Automatic Reference Counting]. Swift is a fast and powerful programming language that is easier and more fun to implement.


=Playgrounds=
=Playgrounds=
Playgrounds enable the interactive experience of a script language, even though Swift compiles to highly-optimized native code. Type a line of code and the result appears immediately. If your code runs over time, for instance through a loop, you can watch its progress in the Timeline assistant. The Timeline displays variables in a graph, draws each step when composing a view, and can play an animated SpriteKit scene.
Playgrounds<ref>[http://www.raywenderlich.com/74438/swift-tutorial-a-quick-start Playgrounds]</ref> enable the interactive experience of a script language, even though Swift compiles to highly-optimized native code. Type a line of code and the result appears immediately. If the code runs over time, for instance through a loop, its progress can be seen in the Timeline assistant. The Timeline displays variables in a graph, draws each step when composing a view, and can play an animated SpriteKit scene.


===Getting Started with Playgrounds===
===Getting Started with Playgrounds===
On opening Xcode6, select "Get started with a Playground". Default is a Hello Playgrounds program.
On opening Xcode6, select "Get started with a Playground". Default is a Hello Playgrounds program.<ref>[https://www.youtube.com/watch?v=A0C6L4XmrZM Introduction to Swift Playground]</ref>
[[File: pgstart.png|frame|center]]
[[File: pgstart.png|frame|center]]


Line 42: Line 42:


==More About Playgrounds==
==More About Playgrounds==
Playgrounds files are used for testing and prototyping code. The results in the sidebar are displayed in black but during the modification of the code, the results temporarily change to gray during processing. ImageView can be used to display images.
Playgrounds files are used for testing and prototyping code. The results in the sidebar are displayed in black but during the modification of the code, the results temporarily change to gray during processing. ImageView can be used to display images<ref>[http://www.sitepoint.com/introduction-swift-programming-language/ More about Playground]</ref>


[[File: codesnippet.jpg|frame|center]]
[[File: codesnippet.jpg|frame|center]]
Line 48: Line 48:


===Read-Eval-Print-Loop (REPL)===
===Read-Eval-Print-Loop (REPL)===
REPL (Read Evaluate Print Loop) is an interactive version of Swift. It is a procedure that loops in following iterations:<br>
REPL (Read Evaluate Print Loop)<ref>[http://swiftlanguagetutorials.com/apple-swift-language-code-menu/apple-swift-language-faq/205-swift-language-faq-what-is-an-xcode-playground REPL]</ref> is an interactive version of Swift. It is a procedure that loops in following iterations:<br>
''Read'':  Read is called and characters in user input are read.<br>
''Read'':  Read is called and characters in user input are read.<br>
''Eval(uate)'': The input is then evaluated and returned<br>
''Eval(uate)'': The input is then evaluated and returned<br>
Line 55: Line 55:


===Viewing Result===
===Viewing Result===
To view graphical display of results, like images or strings, you can click on the Quick Look ([[File:quicklook.png|quicklook.png]] ) button. Access to Quick Look buttons can be gained by moving the pointer along the right edge of the sidebar.
To view graphical display of results, like images or strings, click on the Quick Look ([[File:quicklook.png|quicklook.png]] ) button. Access to Quick Look buttons can be gained by moving the pointer along the right edge of the sidebar.<ref>[https://developer.apple.com/library/ios/recipes/xcode_help-source_editor/chapters/ExploringandEvaluatingSwiftCodeinaPlayground.html  View Results in Playground]</ref>


[[File:swift.jpg|frame|center]]
[[File:swift.jpg|frame|center]]
Line 65: Line 65:
===Timeline===
===Timeline===
The progress of results can be seen on the timeline. The time limit can also be set for the timeline to play back values. An item can be collapsed or expanded by clicking on the expression at the top of the item. An item can be removed from the timeline by clicking on the close button on the upper left corner of the item. The timeline can be closed by clicking on the close button on the Assistant Editor jump bar. It can be opened by clicking on the Assistant Editor button in the workspace toolbar or by clicking any Value History button.
The progress of results can be seen on the timeline. The time limit can also be set for the timeline to play back values. An item can be collapsed or expanded by clicking on the expression at the top of the item. An item can be removed from the timeline by clicking on the close button on the upper left corner of the item. The timeline can be closed by clicking on the close button on the Assistant Editor jump bar. It can be opened by clicking on the Assistant Editor button in the workspace toolbar or by clicking any Value History button.
The value of iterator in a loop can be seen by viewing the timeline. The timeline can display different types of items depending on the input. The value of iterator is shown as graph where the x-axis represents the execution time in seconds and the y-axis represents different values for the iterator. The graph changes on changing the value of the iterator:
The value of iterator in a loop can be seen by viewing the timeline. The timeline can display different types of items depending on the input. The value of iterator is shown as graph where the x-axis represents the execution time in seconds and the y-axis represents different values for the iterator. The graph changes on changing the value of the iterator<ref>[https://www.youtube.com/watch?v=F9T551Y0mYo Timeline]</ref>:


[[File:assistantpic5.png|frame|center]]
[[File:assistantpic5.png|frame|center]]
Line 74: Line 74:
[[File:swiftpic5.png|frame|center]]
[[File:swiftpic5.png|frame|center]]


===Limitations of Playground===
===Limitations of Playground<ref>[http://rshankar.com/xcode-6-and-playground/ Limitations of Playground]</ref>===
i      Playgrounds should not be used for performance testing as logging of results will dominate the runtime. <br>
i      Playgrounds should not be used for performance testing as logging of results will dominate the runtime. <br>
ii    Playground does not support On-device execution. <br>
ii    Playground does not support On-device execution. <br>
Line 86: Line 86:
ii. It was reported by 9to5Mac that a developer discovered that the application is not sandboxed, i.e., Swift code entered in a playground has access to the entire computer sytem. The realtime nature of Playgrounds means that you don't even have to press ENTER to start the erasing process!<ref name= "issues in beta playground">[http://swiftlanguagetutorials.com/apple-swift-language-code-menu/apple-swift-language-faq/205-swift-language-faq-what-is-an-xcode-playground Issues in beta Playground]</ref>
ii. It was reported by 9to5Mac that a developer discovered that the application is not sandboxed, i.e., Swift code entered in a playground has access to the entire computer sytem. The realtime nature of Playgrounds means that you don't even have to press ENTER to start the erasing process!<ref name= "issues in beta playground">[http://swiftlanguagetutorials.com/apple-swift-language-code-menu/apple-swift-language-faq/205-swift-language-faq-what-is-an-xcode-playground Issues in beta Playground]</ref>


=Recent Updates=
=Comparison of Swift with Objective-C=
On September 9th 2014, Apple published in their developer's blog that 'Swift Has Reached 1.0'
 
A comparison of  Xcode Objective-C and Xcode6 with Swift <ref>[http://blog.shinetech.com/2014/07/22/swift-from-an-objective-c-developers-perspective/ Swift and Objective C]</ref>
*In Swift unlike Objective-C, type annotations are not required. The Swift compiler infers the data type and as a result the code size is reduced.
*Objective-C provides dynamic runtime, however, in Swift, the compiler checks all the types when a method is called; instead of dynamic invocation. This enables to find more errors and at the same time increases the speed.
*Unlike Objective-C, Swift uses closures instead of blocks to pass functions. Closures are easy to write and read.
* Instead of using “nil” as in Objective-C, “optionals” are introduces in Swift to handle absence of a value. In Objective-C only objects can be nil but in Swift any value can be set to nil.
*Swift introduces generics that were not present in Objective-C. With generics,  a placeholder can be used first and its specific type can be defined later.
 
In summary, Swift combines the advantages of Objective-C and modern scripting languages. It is faster than Objective-C. It has clean code and powerful debugging tools.
 
=Comparison of Swift with Python=
 
Apple’s developers used Python as a benchmark against Swift and Objective-C. A lot of discussion is on-going about the comparison between Python and Swift. This comparison is possibly because of the ease of development and readability that Swift provides. This expressiveness in Swift is similar to Python. At the same time Swift is compiled and is much faster. While a lot of people are excited about the ease of programming and the performance that Swift provides, there are others who think that Swift still has a long way to go before it can be compared with something as powerful as Python.
 
 
=Release History=
{| class="wikitable" style="font-size: 100%; text-align: center; width: auto;"
 
|-
! Date
! Release Notes
 
|-
| 6/2/2014
| Swift Launched
 
|-
| 9/9/2014
| Swift version 1.0 is now GM
 
|}


=See Also=
=See Also=
Line 94: Line 124:


=References=
=References=
* http://www.raywenderlich.com/74438/swift-tutorial-a-quick-start<br>
* https://developer.apple.com/library/prerelease/ios/recipes/xcode_help-source_editor/chapters/ExploringandEvaluatingSwiftCodeinaPlayground.html#//apple_ref/doc/uid/TP40009975-CH26-SW1<br>
* http://swiftlanguagetutorials.com/apple-swift-language-code-menu/apple-swift-language-faq/205-swift-language-faq-what-is-an-xcode-playground<br>
* http://rshankar.com/xcode-6-and-playground/<br>
* https://developer.apple.com/swift/<br>
* https://www.youtube.com/watch?v=A0C6L4XmrZM<br>
* https://www.youtube.com/watch?v=F9T551Y0mYo<br>
* http://www.youtube.com/watch?v=sp8-jf20HNA<br>
* https://www.youtube.com/watch?v=xtZl1_zc3gg<br>
* http://blog.teamtreehouse.com/an-absolute-beginners-guide-to-swift<br>
* http://www.sitepoint.com/introduction-swift-programming-language/
<references/>
<references/>

Latest revision as of 03:20, 26 September 2014

Playgrounds with Swift in XCode v6.0

Fig 1: Playgrounds with Swift in Xcode v6.0
Fig 1: Playgrounds with Swift in Xcode v6.0

On June 2, 2014 at the World Wide Developers Conference, Apple announced version 6 of Xcode. Features include Playgrounds live debugging tools, as well as an entirely new programming language called Swift. A key element of the Swift system is its ability to be cleanly debugged and run within the development environment, using a read–eval–print loop (REPL)<ref>Swift</ref>, giving it interactive properties more in common with scripting systems like Python than traditional systems programming languages. The REPL is further enhanced with the new 'playgrounds' concept; 'playgrounds' are interactive views running within the Xcode environment that respond to code or debugger changes on-the-fly. If the code in question changes over time or with regard to some other ranged input value, the view can be used with the Timeline Assistant to demonstrate the output in an animated fashion.

Apple claims that Swift "is the first industrial-quality systems programming language that is as expressive and enjoyable as a scripting language."<ref name="Topic">Topic Document</ref>


Background

Xcode

Xcode is an Integrated Development Environment (IDE) developed by Apple for developing software for iOS and OS X. The Xcode suite consists of the IDE, Apple’s developer documentation and a built-in interface builder. It is shipped free with Mac OS X 10.3, but is able to develop OS X applications that can run on any version of OS X. It extends and replaces Apple's earlier tool, Project Builder, which was inherited from NeXT. However, Xcode officially does not work on Mac OS X 10.2.It works hand in hand with Interface Builder (also inherited from NeXT), a graphical tool used to create user interfaces.

Xcode includes GCC, and can compile C, C++, Objective C++, Java, and Objective C source code with a variety of programming models, including but not limited to Cocoa, Carbon, and Java. The latest Xcode version 6 includes Playgrounds which has an interactive sidebar to display the results of the code on-the-fly.

Swift

Swift<ref>Introduction to Swift</ref> is a new programming language that adopts safe programming patterns and makes programming flexible. Swift combines the best of both C and Objective-C. It integrates both - the procedural features like types and operators as well as the object-oriented features such as classes and protocols. It adopts readability and dynamic object model from Objective-C and has added new features like blocks, collection literals and modules, fast performance. Swift has many advantages over Objective-C, like being more type-safe, having more natural and concise syntax, no need for header/implementation files. It has simplified memory management with Automatic Reference Counting. Swift is a fast and powerful programming language that is easier and more fun to implement.

Playgrounds

Playgrounds<ref>Playgrounds</ref> enable the interactive experience of a script language, even though Swift compiles to highly-optimized native code. Type a line of code and the result appears immediately. If the code runs over time, for instance through a loop, its progress can be seen in the Timeline assistant. The Timeline displays variables in a graph, draws each step when composing a view, and can play an animated SpriteKit scene.

Getting Started with Playgrounds

On opening Xcode6, select "Get started with a Playground". Default is a Hello Playgrounds program.<ref>Introduction to Swift Playground</ref>

Playgrounds with Swift introduce user friendly, clean code. Writing code in poetry-method has become simpler with Swift. Inclusion of packages is not necessary and heavy syntax can be ignored. Creation of class is not required while writing code in Swift. Writing "str" in Swift would display the value of "str" on Playground directly. Semicolons are not required at the end of each line. Datatype can be stated in the code, but it is not required as Swift implicitly assigns the datatype.


If the datatype of a variable is explicitly stated, the variable cannot be assigned a value that does not belong to its datatype. Such an assignment results in an error. Swift introduces a new keyword "let" for declaring constants. If the value of a constant declared with "let" is changed, an error is displayed. While iterating through a loop in Swift, the value of the iterator is shown on the console on-the-fly.

More About Playgrounds

Playgrounds files are used for testing and prototyping code. The results in the sidebar are displayed in black but during the modification of the code, the results temporarily change to gray during processing. ImageView can be used to display images<ref>More about Playground</ref>


Read-Eval-Print-Loop (REPL)

REPL (Read Evaluate Print Loop)<ref>REPL</ref> is an interactive version of Swift. It is a procedure that loops in following iterations:
Read: Read is called and characters in user input are read.
Eval(uate): The input is then evaluated and returned
Print: The evaluated result in printed in a textual representation for the users
REPL in Swift enables the user to evaluate and interact with his code in a script-like environment.

Viewing Result

To view graphical display of results, like images or strings, click on the Quick Look (quicklook.png ) button. Access to Quick Look buttons can be gained by moving the pointer along the right edge of the sidebar.<ref>View Results in Playground</ref>

Assistant Editor button can be used to examine values that change throughout the execution of the code.
i. Interim value of a code like for loop can be viewed through Value History button ()
ii. Live view item can be used to view the drawing results. Live view item draws itself in response to the code.

Timeline

The progress of results can be seen on the timeline. The time limit can also be set for the timeline to play back values. An item can be collapsed or expanded by clicking on the expression at the top of the item. An item can be removed from the timeline by clicking on the close button on the upper left corner of the item. The timeline can be closed by clicking on the close button on the Assistant Editor jump bar. It can be opened by clicking on the Assistant Editor button in the workspace toolbar or by clicking any Value History button. The value of iterator in a loop can be seen by viewing the timeline. The timeline can display different types of items depending on the input. The value of iterator is shown as graph where the x-axis represents the execution time in seconds and the y-axis represents different values for the iterator. The graph changes on changing the value of the iterator<ref>Timeline</ref>:

Error

The error messages in Playground is shown as a ! icon. The details about the error can be viewed by clicking on this icon. The image below shows the error message that will be displayed if a variable declared as a string is assigned an integer value.

Limitations of Playground<ref>Limitations of Playground</ref>

i Playgrounds should not be used for performance testing as logging of results will dominate the runtime.
ii Playground does not support On-device execution.
iii Does not support custom entitlements.
iv Playgrounds can not be used for things that require user-interactions

Issues in Beta Playground

i. There are some flaws in beta release which can harm the system of user, like erasing hard drive.

ii. It was reported by 9to5Mac that a developer discovered that the application is not sandboxed, i.e., Swift code entered in a playground has access to the entire computer sytem. The realtime nature of Playgrounds means that you don't even have to press ENTER to start the erasing process!<ref name= "issues in beta playground">Issues in beta Playground</ref>

Comparison of Swift with Objective-C

A comparison of Xcode Objective-C and Xcode6 with Swift <ref>Swift and Objective C</ref>

  • In Swift unlike Objective-C, type annotations are not required. The Swift compiler infers the data type and as a result the code size is reduced.
  • Objective-C provides dynamic runtime, however, in Swift, the compiler checks all the types when a method is called; instead of dynamic invocation. This enables to find more errors and at the same time increases the speed.
  • Unlike Objective-C, Swift uses closures instead of blocks to pass functions. Closures are easy to write and read.
  • Instead of using “nil” as in Objective-C, “optionals” are introduces in Swift to handle absence of a value. In Objective-C only objects can be nil but in Swift any value can be set to nil.
  • Swift introduces generics that were not present in Objective-C. With generics, a placeholder can be used first and its specific type can be defined later.

In summary, Swift combines the advantages of Objective-C and modern scripting languages. It is faster than Objective-C. It has clean code and powerful debugging tools.

Comparison of Swift with Python

Apple’s developers used Python as a benchmark against Swift and Objective-C. A lot of discussion is on-going about the comparison between Python and Swift. This comparison is possibly because of the ease of development and readability that Swift provides. This expressiveness in Swift is similar to Python. At the same time Swift is compiled and is much faster. While a lot of people are excited about the ease of programming and the performance that Swift provides, there are others who think that Swift still has a long way to go before it can be compared with something as powerful as Python.


Release History

Date Release Notes
6/2/2014 Swift Launched
9/9/2014 Swift version 1.0 is now GM

See Also

References

<references/>