Jump to content

Cocoa (API): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m Added comma.
Tags: Mobile edit Mobile app edit iOS app edit
→‎History: +style
 
(28 intermediate revisions by 19 users not shown)
Line 14: Line 14:
| genre = [[Software framework]]
| genre = [[Software framework]]
| license = [[Proprietary software|Proprietary]]<br />with some open source components{{citation needed|date=June 2016}}
| license = [[Proprietary software|Proprietary]]<br />with some open source components{{citation needed|date=June 2016}}
| website = {{URL|developer.apple.com/technologies/mac/cocoa.html|Apple Developer}}
| website = {{URL|developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/WhatIsCocoa/WhatIsCocoa.html|Apple Developer}}
}}
}}
'''Cocoa''' is [[Apple Inc.|Apple]]'s native [[object-oriented programming|object-oriented]] [[application programming interface]] (API) for its [[Desktop computer|desktop]] [[operating system]] [[macOS]].
'''Cocoa''' is [[Apple Inc.|Apple]]'s native [[object-oriented programming|object-oriented]] [[application programming interface]] (API) for its [[Desktop computer|desktop]] [[operating system]] [[macOS]].
Line 20: Line 20:
Cocoa consists of the [[Foundation Kit]], [[Application Kit]], and [[Core Data]] frameworks, as included by the <code>Cocoa.h</code> header file, and the libraries and frameworks included by those, such as the C standard library and the Objective-C runtime.<ref name="apple1">[https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/OSX_Technology_Overview/SystemFrameworks/SystemFrameworks.html Mac Technology Overview: OS X Frameworks]. Developer.apple.com. Retrieved on September 18, 2013.</ref>
Cocoa consists of the [[Foundation Kit]], [[Application Kit]], and [[Core Data]] frameworks, as included by the <code>Cocoa.h</code> header file, and the libraries and frameworks included by those, such as the C standard library and the Objective-C runtime.<ref name="apple1">[https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/OSX_Technology_Overview/SystemFrameworks/SystemFrameworks.html Mac Technology Overview: OS X Frameworks]. Developer.apple.com. Retrieved on September 18, 2013.</ref>


Cocoa applications are typically developed using the development tools provided by Apple, specifically [[Xcode]] (formerly [[Project Builder]]) and [[Interface Builder]] (now part of Xcode), using the [[programming language]]s [[Objective-C]] or [[Swift (programming language)|Swift]]. However, the Cocoa programming environment can be accessed using other tools, such as [[Clozure CL]], [[LispWorks]], [[Object Pascal]], [[Python (programming language)|Python]], [[Perl]], [[Ruby (programming language)|Ruby]], and [[AppleScript]] with the aid of [[language binding|bridge mechanisms]] such as [[PasCocoa]], [[PyObjC]], [[CamelBones]], [[RubyCocoa]], and a [[D (programming language)|D]]/Objective-C Bridge. A Ruby language implementation named [[MacRuby]], which removes the need for a bridge mechanism, was formerly developed by Apple, while [[Nu (programming language)|Nu]] is a [[Lisp (programming language)|Lisp]]-like language that can be used with Cocoa with no bridge. It is also possible to write Objective-C Cocoa programs in a simple [[text editor]] and build it manually with [[GNU Compiler Collection]] (GCC) or [[Clang]] from the command line or from a [[Make (software)|makefile]].
Cocoa applications are typically developed using the development tools provided by Apple, specifically [[Xcode]] (formerly [[Project Builder]]) and [[Interface Builder]] (now part of Xcode), using the [[programming language]]s [[Objective-C]] or [[Swift (programming language)|Swift]]. However, the Cocoa programming environment can be accessed using other tools. It is also possible to write Objective-C Cocoa programs in a simple [[text editor]] and build it manually with [[GNU Compiler Collection]] (GCC) or [[Clang]] from the command line or from a [[Make (software)|makefile]].


For [[End-user (computer science)|end-users]], Cocoa [[application software|applications]] are those written using the Cocoa programming environment. Such applications usually have a familiar look and feel, since the Cocoa programming environment automates many aspects of an application to comply with Apple's [[human interface guidelines]].
For [[End user|end users]], Cocoa [[application software|applications]] are those written using the Cocoa programming environment. Such applications usually have a familiar look and feel, since the Cocoa programming environment provides a lot of common [[User interface|UI elements]] (such as buttons, scroll bars, etc.), and automates many aspects of an application to comply with Apple's [[human interface guidelines]].


For [[iOS]], [[iPadOS]], [[tvOS]], and [[watchOS]], a similar API exists, named [[Cocoa Touch]], which includes [[gesture recognition]], [[animation]], and a different set of [[graphical control element]]s. It is used in [[application software|applications]] for Apple devices such as the [[iPhone]], the [[iPod Touch]], the [[iPad]], the [[Apple TV]], and the [[Apple Watch]].
For [[iOS]], [[iPadOS]], [[tvOS]], and [[watchOS]], a similar API exists, named [[Cocoa Touch]], which includes [[gesture recognition]], [[animation]], and a different set of [[graphical control element]]s. It is used in [[application software|applications]] for Apple devices such as the [[iPhone]], the [[iPod Touch]], the [[iPad]], the [[Apple TV]], and the [[Apple Watch]].
Line 32: Line 32:
Much of the work that went into developing OpenStep was applied to developing Mac OS X, Cocoa being the most visible part. However, differences exist. For example, NeXTSTEP and OpenStep used [[Display PostScript]] for on-screen display of text and graphics, while Cocoa depends on Apple's [[Quartz (graphics layer)|Quartz]] (which uses the [[Portable Document Format]] (PDF) imaging model, but not its underlying technology). Cocoa also has a level of Internet support, including the NSURL and [[WebKit]] [[HTML]] classes, and others, while OpenStep had only rudimentary support for managed network connections via NSFileHandle classes and [[Berkeley sockets]].
Much of the work that went into developing OpenStep was applied to developing Mac OS X, Cocoa being the most visible part. However, differences exist. For example, NeXTSTEP and OpenStep used [[Display PostScript]] for on-screen display of text and graphics, while Cocoa depends on Apple's [[Quartz (graphics layer)|Quartz]] (which uses the [[Portable Document Format]] (PDF) imaging model, but not its underlying technology). Cocoa also has a level of Internet support, including the NSURL and [[WebKit]] [[HTML]] classes, and others, while OpenStep had only rudimentary support for managed network connections via NSFileHandle classes and [[Berkeley sockets]].


The resulting software framework received the name ''Cocoa'' for the sake of expediency, because the name had already been trademarked by Apple. For many years before this present use of the name, Apple's ''Cocoa'' trademark had originated as the name of a multimedia project design application for children. The application was [[Stagecast Creator#History|originally developed]] at the [[Apple Advanced Technology Group]] under the name ''KidSim'', and was then renamed and trademarked as "Cocoa". The name, coined by Peter Jensen who was hired to develop Cocoa for Apple, was intended to evoke "Java for kids", as it ran embedded in web pages.<ref>{{cite news|last1=Mardesich|first1=Jodi|title=A Sour Note in Apple's Rhapsody Once-Loyal Software Writers Wary of New OS as Crucial Conference Looms|url=http://nl.newsbank.com/nl-search/we/Archives?p_product=SJ&p_theme=sj&p_action=search&p_maxdocs=200&s_dispstring=allfields(Peter%20Jensen)%20AND%20date(1/1/1997%20to%201/1/1998)&p_field_date-0=YMD_date&p_params_date-0=date:B,E&p_text_date-0=1/1/1997%20to%201/1/1998)&p_field_advanced-0=&p_text_advanced-0=(%22Peter%20Jensen%22)&xcal_numdocs=20&p_perpage=10&p_sort=YMD_date:D&xcal_useweights=no|access-date=13 August 2015|agency=San Jose Mercury News|issue=Morning Final|date=April 14, 1997}}</ref> The trademark, and thus the name "Cocoa", was re-used to avoid the delay which would have occurred while registering a new [[trademark]] for this software framework. The original "Cocoa" program was discontinued at Apple in one of the [[rationalization (economics)|rationalizations]] that followed [[Steve Jobs]]'s return to Apple. It was then licensed to a third party and marketed as [[Stagecast Creator]] {{as of|2011|lc=on}}.
The API toolbox was originally called “Yellow Box” and was renamed to Cocoa - a name that had been already trademarked by Apple. Apple's ''Cocoa'' trademark had originated as the name of a multimedia project design application for children. The name was intended to evoke "Java for kids", as it ran embedded in web pages.<ref>{{cite news|last1=Mardesich|first1=Jodi|title=A Sour Note in Apple's Rhapsody Once-Loyal Software Writers Wary of New OS as Crucial Conference Looms|url=http://nl.newsbank.com/nl-search/we/Archives?p_product=SJ&p_theme=sj&p_action=search&p_maxdocs=200&s_dispstring=allfields(Peter%20Jensen)%20AND%20date(1/1/1997%20to%201/1/1998)&p_field_date-0=YMD_date&p_params_date-0=date:B,E&p_text_date-0=1/1/1997%20to%201/1/1998)&p_field_advanced-0=&p_text_advanced-0=(%22Peter%20Jensen%22)&xcal_numdocs=20&p_perpage=10&p_sort=YMD_date:D&xcal_useweights=no|access-date=13 August 2015|agency=San Jose Mercury News|issue=Morning Final|date=April 14, 1997|archive-url=https://web.archive.org/web/20160306122132/http://nl.newsbank.com/nl-search/we/Archives?p_product=SJ&p_theme=sj&p_action=search&p_maxdocs=200&s_dispstring=allfields(Peter%20Jensen)%20AND%20date(1/1/1997%20to%201/1/1998)&p_field_date-0=YMD_date&p_params_date-0=date:B,E&p_text_date-0=1/1/1997%20to%201/1/1998)&p_field_advanced-0=&p_text_advanced-0=(%22Peter%20Jensen%22)&xcal_numdocs=20&p_perpage=10&p_sort=YMD_date:D&xcal_useweights=no|archive-date=2016-03-06|url-status=dead}}</ref> The original "Cocoa" program was discontinued following the return of [[Steve Jobs]] to Apple. At the time, Java was a big focus area for the company, so “Cocoa” was used as the new name for “Yellow Box” because, in addition to the native Objective-C usage, it could also be accessed from Java via a bridging layer.<ref>{{Citation |last=Apple Inc. |title=WWDC 1999 |date=1999-05-10 |url=http://archive.org/details/1999-05-10-wwdc-keynote-bad-audio |access-date=2024-04-27}}</ref> Even though Apple discontinued support for the Cocoa Java bridge, the name continued and was even used for the [[Cocoa Touch]] API.


==Memory management==
==Memory management==
Line 44: Line 44:


==Main frameworks==
==Main frameworks==

Cocoa consists of three [[Objective-C]] object libraries called ''[[Application framework|frameworks]]''. Frameworks are functionally similar to [[Library (computer science)|shared libraries]], a compiled object that can be dynamically loaded into a program's address space at runtime, but frameworks add associated resources, header files, and documentation. The Cocoa frameworks are implemented as a type of [[bundle (macOS)|bundle]], containing the aforementioned items in standard locations.
Cocoa consists of three [[Objective-C]] object libraries called ''[[Application framework|frameworks]]''. Frameworks are functionally similar to [[Library (computer science)|shared libraries]], a compiled object that can be dynamically loaded into a program's address space at runtime, but frameworks add associated resources, header files, and documentation. The Cocoa frameworks are implemented as a type of [[bundle (macOS)|bundle]], containing the aforementioned items in standard locations.


Line 53: Line 52:
A key part of the Cocoa architecture is its comprehensive views model. This is organized along conventional lines for an application framework, but is based on the [[Portable Document Format]] (PDF) drawing model provided by [[Quartz (graphics layer)|Quartz]]. This allows creating custom drawing content using [[PostScript]]-like drawing commands, which also allows automatic printer support and so forth. Since the Cocoa framework manages all the clipping, scrolling, scaling and other chores of drawing graphics, the programmer is freed from implementing basic infrastructure and can concentrate on the unique aspects of an application's content.
A key part of the Cocoa architecture is its comprehensive views model. This is organized along conventional lines for an application framework, but is based on the [[Portable Document Format]] (PDF) drawing model provided by [[Quartz (graphics layer)|Quartz]]. This allows creating custom drawing content using [[PostScript]]-like drawing commands, which also allows automatic printer support and so forth. Since the Cocoa framework manages all the clipping, scrolling, scaling and other chores of drawing graphics, the programmer is freed from implementing basic infrastructure and can concentrate on the unique aspects of an application's content.


==Model–view–controller==
==Model-view-controller==
{{main article|Model-view-controller}}
{{Main article|Model–view–controller}}


The [[Smalltalk]] teams at [[Xerox PARC]] eventually settled on a design philosophy that led to easy development and high code reuse. Named ''[[model-view-controller]]'' (MVC), the concept breaks an application into three sets of interacting object classes:
The [[Smalltalk]] teams at [[Xerox PARC]] eventually settled on a design philosophy that led to easy development and high code reuse. Named ''[[model–view–controller]]'' (MVC), the concept breaks an application into three sets of interacting object classes:


* Model classes represent problem domain data and operations (such as lists of people/departments/budgets; documents containing sections/paragraphs/footnotes of stylized text).
* Model classes represent problem domain data and operations (such as lists of people/departments/budgets; documents containing sections/paragraphs/footnotes of stylized text).
Line 75: Line 74:
Under Objective-C, methods are represented by a ''selector'', a string describing the method to call. When a message is sent, the selector is sent into the Objective-C runtime, matched against a list of available methods, and the method's ''implementation'' is called. Since the selector is text data, this lets it be saved to a file, transmitted over a network or between processes, or manipulated in other ways. The implementation of the method is looked up at runtime, not compile time. There is a small performance penalty for this,<ref>[[Wikibooks:Programming Mac OS X with Cocoa for beginners/Objective C, the language and its advantages#Some Objective-C advantages|Wikibooks - Some Objective-C advantages]]</ref> but late binding allows the same selector to reference different implementations.
Under Objective-C, methods are represented by a ''selector'', a string describing the method to call. When a message is sent, the selector is sent into the Objective-C runtime, matched against a list of available methods, and the method's ''implementation'' is called. Since the selector is text data, this lets it be saved to a file, transmitted over a network or between processes, or manipulated in other ways. The implementation of the method is looked up at runtime, not compile time. There is a small performance penalty for this,<ref>[[Wikibooks:Programming Mac OS X with Cocoa for beginners/Objective C, the language and its advantages#Some Objective-C advantages|Wikibooks - Some Objective-C advantages]]</ref> but late binding allows the same selector to reference different implementations.


By a similar token, Cocoa provides a pervasive data manipulation method called ''key-value coding'' (KVC).<ref>[https://developer.apple.com/documentation/Cocoa/Conceptual/KeyValueCoding/ Key-Value Coding Programming Guide: Introduction]</ref> This allows a piece of data or property of an object to be looked up or changed at runtime by name. The property name acts as a key to the value. In traditional languages, this late binding is impossible. KVC leads to great design flexibility. An object's type need not be known, yet any property of that object can be discovered using KVC. Also, by extending this system using something Cocoa terms ''key-value observing'' (KVO), automatic support for [[undo|undo-redo]] is provided.
By a similar token, Cocoa provides a pervasive data manipulation method called ''key-value coding'' (KVC).<ref>{{Cite web |url=https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/KeyValueCoding/ |title=Key-Value Coding Programming Guide |access-date=September 27, 2021}}</ref> This allows a piece of data or property of an object to be looked up or changed at runtime by name. The property name acts as a key to the value. In traditional languages, this late binding is impossible. KVC leads to great design flexibility. An object's type need not be known, yet any property of that object can be discovered using KVC. Also, by extending this system using something Cocoa terms ''key-value observing'' (KVO), automatic support for [[undo|undo-redo]] is provided.


Late static binding is a variant of binding somewhere between static and dynamic binding. The binding of names before the program is run is called static (''early''); bindings performed as the program runs are dynamic (''late'' or ''virtual'').
Late static binding is a variant of binding somewhere between static and dynamic binding. The binding of names before the program is run is called static (''early''); bindings performed as the program runs are dynamic (''late'' or ''virtual'').


==Rich objects==
==Rich objects==

One of the most useful features of Cocoa is the powerful ''base objects'' the system supplies. As an example, consider the Foundation classes <code>NSString</code> and <code>NSAttributedString</code>, which provide [[Unicode]] [[string (computer science)|strings]], and the <code>[[NSText]]</code> system in AppKit, which allows the programmer to place string objects in the GUI.
One of the most useful features of Cocoa is the powerful ''base objects'' the system supplies. As an example, consider the Foundation classes <code>NSString</code> and <code>NSAttributedString</code>, which provide [[Unicode]] [[string (computer science)|strings]], and the <code>[[NSText]]</code> system in AppKit, which allows the programmer to place string objects in the GUI.


Line 87: Line 85:


==Implementations and bindings==
==Implementations and bindings==
The Cocoa frameworks are written in [[Objective-C]]. Java [[language binding|bindings]] for the Cocoa frameworks (termed the ''Java bridge'') were also made available with the aim of replacing Objective-C with a more popular language<ref>{{cite journal|url=http://www.mactech.com/articles/mactech/Vol.19/19.12/CocoaAppsinJava/index.html|title=Writing Cocoa Apps in Java|author=Steve Klingsporn|journal=[[MacTech]]|volume=19|issue=12|year=2003}}</ref> but these bindings were unpopular among Cocoa developers and Cocoa's message passing semantics did not translate well to a statically-typed language such as Java.<ref>{{cite web|url=https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/Legacy/JavaBridge/JavaBridge.pdf|title=Using the Java Bridge|publisher=[[Apple Inc.]]|quote=Because Java is a strongly typed language, it requires more information about the classes and interfaces it manipulates at compile time. Therefore, before using Objective-C classes as Java ones, a description of them has to be written and compiled.}}</ref> Cocoa's need for runtime binding means many of Cocoa's key features are not available with Java. In 2005, Apple announced that the Java bridge was to be deprecated, meaning that features added to Cocoa in macOS versions later than 10.4 would not be added to the Cocoa-Java programming interface.

The Cocoa frameworks are written in [[Objective-C]], and hence that is the preferred language for developing Cocoa applications.{{Citation needed|date=April 2015}} Java [[language binding|bindings]] for the Cocoa frameworks (termed the ''Java bridge'') were also made available with the aim of replacing Objective-C with a more popular language<ref>{{cite journal|url=http://www.mactech.com/articles/mactech/Vol.19/19.12/CocoaAppsinJava/index.html|title=Writing Cocoa Apps in Java|author=Steve Klingsporn|journal=[[MacTech]]|volume=19|issue=12|year=2003}}</ref> but these bindings were unpopular among Cocoa developers and Cocoa's message passing semantics did not translate well to a statically-typed language such as Java.<ref>{{cite web|url=https://developer.apple.com/legacy/library/documentation/Cocoa/Conceptual/Legacy/JavaBridge/JavaBridge.pdf|title=Using the Java Bridge|publisher=[[Apple Inc.]]|quote=Because Java is a strongly typed language, it requires more information about the classes and interfaces it manipulates at compile time. Therefore, before using Objective-C classes as Java ones, a description of them has to be written and compiled.}}</ref> Cocoa's need for runtime binding means many of Cocoa's key features are not available with Java. In 2005, Apple announced that the Java bridge was to be deprecated, meaning that features added to Cocoa in macOS versions later than 10.4 would not be added to the Cocoa-Java programming interface.


At [[Apple Worldwide Developers Conference]] (WWDC) 2014, Apple introduced a new programming language named [[Swift (programming language)|Swift]], which is intended to replace Objective-C.<ref>{{cite web |url=http://appleinsider.com/articles/14/06/04/apples-top-secret-swift-language-grew-from-work-to-sustain-objective-c-which-it-now-aims-to-replace |title=Apple's top secret Swift language grew from work to sustain Objective-C, which it now aims to replace}}</ref>
At [[Apple Worldwide Developers Conference]] (WWDC) 2014, Apple introduced a new programming language named [[Swift (programming language)|Swift]], which is intended to replace Objective-C.<ref>{{cite web |url=http://appleinsider.com/articles/14/06/04/apples-top-secret-swift-language-grew-from-work-to-sustain-objective-c-which-it-now-aims-to-replace |title=Apple's top secret Swift language grew from work to sustain Objective-C, which it now aims to replace}}</ref>
Line 96: Line 93:


===Other bindings===
===Other bindings===

<!--Try to organize bindings, alphabetically by language.-->Third-party bindings available for other languages include [[Clozure CL]], [[Monobjc]] and [[NObjective]] ([[C Sharp (programming language)|C#]]), [[Cocoa Sharp|Cocoa#]] (CLI), Cocodao and [[D (programming language)|D]]/Objective-C Bridge,<ref>[http://sourceforge.net/projects/cocodao/ Cocodao], bridge to create Cocoa applications in D language.</ref><ref>[http://michelf.com/weblog/2007/d-objc-bridge/ D/Objective-C Bridge], a [[language binding]] mechanism for Cocoa.</ref> [[LispWorks]], [[CamelBones]] ([[Perl]]), [[PyObjC]] ([[Python (programming language)|Python]]), [[FPC PasCocoa]] ([[Lazarus (IDE)|Lazarus]] and [[Free Pascal]]), [[RubyCocoa]] ([[Ruby (programming language)|Ruby]]).<ref>[http://osx.hyperjeff.net/Reference/cocoa.php more extensive list of implementations]</ref> [[Nu (programming language)|Nu]] uses the [[Objective-C]] object model directly, and thus can use the Cocoa frameworks without needing a binding.
The Cocoa programming environment can be accessed using other tools with the aid of [[language binding|bridge mechanisms]] such as [[PasCocoa]], [[PyObjC]], [[CamelBones]], [[RubyCocoa]], and a [[D (programming language)|D]]/Objective-C Bridge.

<!--Try to organize bindings, alphabetically by language.-->Third-party bindings available for other languages include [[AppleScript]], [[Clozure CL]], [[Monobjc]] and [[NObjective]] ([[C Sharp (programming language)|C#]]), [[Cocoa Sharp|Cocoa#]] (CLI), Cocodao and [[D (programming language)|D]]/Objective-C Bridge,<ref>[http://sourceforge.net/projects/cocodao/ Cocodao], bridge to create Cocoa applications in D language.</ref><ref>[http://michelf.com/weblog/2007/d-objc-bridge/ D/Objective-C Bridge], a [[language binding]] mechanism for Cocoa.</ref> [[LispWorks]], [[Object Pascal]], [[CamelBones]] ([[Perl]]), [[PyObjC]] ([[Python (programming language)|Python]]), [[FPC PasCocoa]] ([[Lazarus (IDE)|Lazarus]] and [[Free Pascal]]), [[RubyCocoa]] ([[Ruby (programming language)|Ruby]]).<ref>[http://osx.hyperjeff.net/Reference/cocoa.php more extensive list of implementations]</ref>

A Ruby language implementation named [[MacRuby]], which removes the need for a bridge mechanism, was formerly developed by Apple, while [[Nu (programming language)|Nu]] is a [[Lisp (programming language)|Lisp]]-like language that uses the [[Objective-C]] object model directly, and thus can use the Cocoa frameworks without needing a binding.


===Other implementations===
===Other implementations===
Line 115: Line 117:
* [[X11]]
* [[X11]]
* [[QuickDraw]]
* [[QuickDraw]]
* [[SIMBL]]
* [[Swift (programming language)]]
* [[Swift (programming language)]]
}}
}}
Line 126: Line 129:
*[[Michael Beam]], [[James Duncan Davidson]]: ''Cocoa in a Nutshell'', O'Reilly, 1st Edition 2003, Paperback, {{ISBN|0-596-00462-1}}.
*[[Michael Beam]], [[James Duncan Davidson]]: ''Cocoa in a Nutshell'', O'Reilly, 1st Edition 2003, Paperback, {{ISBN|0-596-00462-1}}.
*[[Erick Tejkowski]]: ''Cocoa Programming for Dummies'', 1st Edition 2003, Paperback, {{ISBN|0-7645-2613-8}}.
*[[Erick Tejkowski]]: ''Cocoa Programming for Dummies'', 1st Edition 2003, Paperback, {{ISBN|0-7645-2613-8}}.
*[[Simson Garfinkel]], [[Michael K. Mahoney]]: ''Building Cocoa Applications: A Step by Step Guide'', O'Reilly, 1st Edition 2002, Paperback, {{ISBN|0-596-00235-1}}.<ref>{{cite book |title= Building Cocoa Applications: A Step by Step Guide |publisher=O'Reilly |date=2002 |first=Simson |last=Garfinkel |first2=Michael K. |last2=Mahoney |url=http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.394.3248&rep=rep1&type=pdf }}</ref>
*{{cite book |title= Building Cocoa Applications: A Step by Step Guide |publisher=[[O'Reilly Media]] |edition=1st |date=2002 |first1=Simson |last1=Garfinkel |author-link1=Simson Garfinkel |first2=Michael K. |last2=Mahoney |isbn=0-596-00235-1 |citeseerx=10.1.1.394.3248 }}
*[[Paris Buttfield-Addison]], [[Jon Manning]]: ''Learning Cocoa with Objective-C'', O'Reilly, 3rd Edition 2012, Paperback, {{ISBN|978-1-4493-1849-9}}.
*[[Paris Buttfield-Addison]], [[Jon Manning]]: ''Learning Cocoa with Objective-C'', O'Reilly, 3rd Edition 2012, Paperback, {{ISBN|978-1-4493-1849-9}}.
*[[Scott Anguish]], [[Erik M. Buck]], [[Donald A. Yacktman]]: ''Cocoa Programming'', Sams, 1st Edition 2002, Paperback, {{ISBN|0-672-32230-7}}.
*[[Scott Anguish]], [[Erik M. Buck]], [[Donald A. Yacktman]]: ''Cocoa Programming'', Sams, 1st Edition 2002, Paperback, {{ISBN|0-672-32230-7}}.
Line 137: Line 140:
* [https://developer.apple.com/library/mac/navigation/#section=Frameworks&topic=Cocoa%20Layer Mac Developer Library, Cocoa Layer], Apple's documentation
* [https://developer.apple.com/library/mac/navigation/#section=Frameworks&topic=Cocoa%20Layer Mac Developer Library, Cocoa Layer], Apple's documentation
* [https://web.archive.org/web/20190202170514/http://www.idevapps.com/ iDevApps], Mac programming forum
* [https://web.archive.org/web/20190202170514/http://www.idevapps.com/ iDevApps], Mac programming forum
* [http://www.cocoadevcentral.com/ Cocoa Dev Central]
* [http://www.cocoadevcentral.com/ Cocoa Dev Central] {{Webarchive|url=https://web.archive.org/web/20201114133049/http://cocoadevcentral.com/ |date=November 14, 2020 }}
* [https://web.archive.org/web/20080801025517/http://www.cocoadev.com/ Cocoa Dev]
* [https://web.archive.org/web/20080801025517/http://www.cocoadev.com/ Cocoa Dev]
* [https://stackoverflow.com/questions/tagged/cocoa Stack Overflow: Cocoa] <!-- Stack Overflow is a popular and well regarded when it comes to Cocoa and Cocoa Touch -->
* [https://stackoverflow.com/questions/tagged/cocoa Stack Overflow: Cocoa] <!-- Stack Overflow is a popular and well regarded when it comes to Cocoa and Cocoa Touch -->
Line 145: Line 148:


[[Category:MacOS APIs]]
[[Category:MacOS APIs]]
[[Category:Apple Inc. developed frameworks]]

Latest revision as of 15:52, 26 May 2024

Cocoa
Developer(s)Apple Inc.
Written inC, C++, Objective-C, Swift
Operating systemmacOS
TypeSoftware framework
LicenseProprietary
with some open source components[citation needed]
WebsiteApple Developer

Cocoa is Apple's native object-oriented application programming interface (API) for its desktop operating system macOS.

Cocoa consists of the Foundation Kit, Application Kit, and Core Data frameworks, as included by the Cocoa.h header file, and the libraries and frameworks included by those, such as the C standard library and the Objective-C runtime.[1]

Cocoa applications are typically developed using the development tools provided by Apple, specifically Xcode (formerly Project Builder) and Interface Builder (now part of Xcode), using the programming languages Objective-C or Swift. However, the Cocoa programming environment can be accessed using other tools. It is also possible to write Objective-C Cocoa programs in a simple text editor and build it manually with GNU Compiler Collection (GCC) or Clang from the command line or from a makefile.

For end users, Cocoa applications are those written using the Cocoa programming environment. Such applications usually have a familiar look and feel, since the Cocoa programming environment provides a lot of common UI elements (such as buttons, scroll bars, etc.), and automates many aspects of an application to comply with Apple's human interface guidelines.

For iOS, iPadOS, tvOS, and watchOS, a similar API exists, named Cocoa Touch, which includes gesture recognition, animation, and a different set of graphical control elements. It is used in applications for Apple devices such as the iPhone, the iPod Touch, the iPad, the Apple TV, and the Apple Watch.

History[edit]

Cocoa continues the lineage of several software frameworks (mainly the App Kit and Foundation Kit) from the NeXTSTEP and OpenStep programming environments developed by NeXT in the 1980s and 1990s. Apple acquired NeXT in December 1996, and subsequently went to work on the Rhapsody operating system that was to be the direct successor of OpenStep. It was to have had an emulation base for classic Mac OS applications, named Blue Box. The OpenStep base of libraries and binary support was termed Yellow Box. Rhapsody evolved into Mac OS X, and the Yellow Box became Cocoa. Thus, Cocoa classes begin with the letters NS, such as NSString or NSArray. These stand for the original proprietary term for the OpenStep framework, NeXTSTEP.[2]

Much of the work that went into developing OpenStep was applied to developing Mac OS X, Cocoa being the most visible part. However, differences exist. For example, NeXTSTEP and OpenStep used Display PostScript for on-screen display of text and graphics, while Cocoa depends on Apple's Quartz (which uses the Portable Document Format (PDF) imaging model, but not its underlying technology). Cocoa also has a level of Internet support, including the NSURL and WebKit HTML classes, and others, while OpenStep had only rudimentary support for managed network connections via NSFileHandle classes and Berkeley sockets.

The API toolbox was originally called “Yellow Box” and was renamed to Cocoa - a name that had been already trademarked by Apple. Apple's Cocoa trademark had originated as the name of a multimedia project design application for children. The name was intended to evoke "Java for kids", as it ran embedded in web pages.[3] The original "Cocoa" program was discontinued following the return of Steve Jobs to Apple. At the time, Java was a big focus area for the company, so “Cocoa” was used as the new name for “Yellow Box” because, in addition to the native Objective-C usage, it could also be accessed from Java via a bridging layer.[4] Even though Apple discontinued support for the Cocoa Java bridge, the name continued and was even used for the Cocoa Touch API.

Memory management[edit]

One feature of the Cocoa environment is its facility for managing dynamically allocated memory. Foundation Kit's NSObject class, from which most classes, both vendor and user, are derived, implements a reference counting scheme for memory management. Objects that derive from the NSObject root class respond to a retain and a release message, and keep a retain count. A method titled retainCount exists, but contrary to its name, will usually not return the exact retain count of an object. It is mainly used for system-level purposes. Invoking it manually is not recommended by Apple.

A newly allocated object created with alloc or copy has a retain count of one. Sending that object a retain message increments the retain count, while sending it a release message decrements the retain count. When an object's retain count reaches zero, it is deallocated by a procedure similar to a C++ destructor. dealloc is not guaranteed to be invoked.

Starting with Objective-C 2.0, the Objective-C runtime implemented an optional garbage collector, which is now obsolete and deprecated in favor of Automatic Reference Counting (ARC). In this model, the runtime turned Cocoa reference counting operations such as "retain" and "release" into no-ops. The garbage collector does not exist on the iOS implementation of Objective-C 2.0. Garbage collection in Objective-C ran on a low-priority background thread, and can halt on Cocoa's user events, with the intention of keeping the user experience responsive. The legacy garbage collector is still available on Mac OS X version 10.13, but no Apple-provided applications use it.

In 2011, the LLVM compiler introduced Automatic Reference Counting (ARC), which replaces the conventional garbage collector by performing static analysis of Objective-C source code and inserting retain and release messages as necessary.

Main frameworks[edit]

Cocoa consists of three Objective-C object libraries called frameworks. Frameworks are functionally similar to shared libraries, a compiled object that can be dynamically loaded into a program's address space at runtime, but frameworks add associated resources, header files, and documentation. The Cocoa frameworks are implemented as a type of bundle, containing the aforementioned items in standard locations.

  • Foundation Kit (Foundation), first appeared in Enterprise Objects Framework on NeXTSTEP 3.[5] It was developed as part of the OpenStep work, and subsequently became the basis for OpenStep's AppKit when that system was released in 1994. On macOS, Foundation is based on Core Foundation. Foundation is a generic object-oriented library providing string and value manipulation, containers and iteration, distributed computing, event loops (run loops), and other functions that are not directly tied to the graphical user interface. The "NS" prefix, used for all classes and constants in the framework, comes from Cocoa's OPENSTEP heritage, which was jointly developed by NeXT and Sun Microsystems.
  • Application Kit (AppKit) is directly descended from the original NeXTSTEP Application Kit. It contains code programs can use to create and interact with graphical user interfaces. AppKit is built on top of Foundation, and uses the same NS prefix.
  • Core Data is the object persistence framework included with Foundation and Cocoa and found in Cocoa.h.[1]

A key part of the Cocoa architecture is its comprehensive views model. This is organized along conventional lines for an application framework, but is based on the Portable Document Format (PDF) drawing model provided by Quartz. This allows creating custom drawing content using PostScript-like drawing commands, which also allows automatic printer support and so forth. Since the Cocoa framework manages all the clipping, scrolling, scaling and other chores of drawing graphics, the programmer is freed from implementing basic infrastructure and can concentrate on the unique aspects of an application's content.

Model–view–controller[edit]

The Smalltalk teams at Xerox PARC eventually settled on a design philosophy that led to easy development and high code reuse. Named model–view–controller (MVC), the concept breaks an application into three sets of interacting object classes:

  • Model classes represent problem domain data and operations (such as lists of people/departments/budgets; documents containing sections/paragraphs/footnotes of stylized text).
  • View classes implement visual representations and affordances for human-computer interaction (such as scrollable grids of captioned icons and pop-up menus of possible operations).
  • Controller classes contain logic that surfaces model data as view representations, maps affordance-initiated user actions to model operations, and maintains state to keep the two synchronized.

Cocoa's design is a fairly, but not absolutely strict application of MVC principles. Under OpenStep, most of the classes provided were either high-level View classes (in AppKit) or one of a number of relatively low-level model classes like NSString. Compared to similar MVC systems, OpenStep lacked a strong model layer. No stock class represented a "document," for instance. During the transition to Cocoa, the model layer was expanded greatly, introducing a number of pre-rolled classes to provide functionality common to desktop applications.

In Mac OS X 10.3, Apple introduced the NSController family of classes, which provide predefined behavior for the controller layer. These classes are considered part of the Cocoa Bindings system, which also makes extensive use of protocols such as Key-Value Observing and Key-Value Binding. The term 'binding' refers to a relationship between two objects, often between a view and a controller. Bindings allow the developer to focus more on declarative relationships rather than orchestrating fine-grained behavior.

With the arrival of Mac OS X 10.4, Apple extended this foundation further by introducing the Core Data framework, which standardizes change tracking and persistence in the model layer. In effect, the framework greatly simplifies the process of making changes to application data, undoing changes when necessary, saving data to disk, and reading it back in.

In providing framework support for all three MVC domains, Apple's goal is to reduce the amount of boilerplate or "glue" code that developers have to write, freeing up resources to spend time on application-specific features.

Late binding[edit]

In most object-oriented languages, calls to methods are represented physically by a pointer to the code in memory. This restricts the design of an application since specific command handling classes are needed, usually organized according to the chain-of-responsibility pattern. While Cocoa retains this approach for the most part, Objective-C's late binding opens up more flexibility.

Under Objective-C, methods are represented by a selector, a string describing the method to call. When a message is sent, the selector is sent into the Objective-C runtime, matched against a list of available methods, and the method's implementation is called. Since the selector is text data, this lets it be saved to a file, transmitted over a network or between processes, or manipulated in other ways. The implementation of the method is looked up at runtime, not compile time. There is a small performance penalty for this,[6] but late binding allows the same selector to reference different implementations.

By a similar token, Cocoa provides a pervasive data manipulation method called key-value coding (KVC).[7] This allows a piece of data or property of an object to be looked up or changed at runtime by name. The property name acts as a key to the value. In traditional languages, this late binding is impossible. KVC leads to great design flexibility. An object's type need not be known, yet any property of that object can be discovered using KVC. Also, by extending this system using something Cocoa terms key-value observing (KVO), automatic support for undo-redo is provided.

Late static binding is a variant of binding somewhere between static and dynamic binding. The binding of names before the program is run is called static (early); bindings performed as the program runs are dynamic (late or virtual).

Rich objects[edit]

One of the most useful features of Cocoa is the powerful base objects the system supplies. As an example, consider the Foundation classes NSString and NSAttributedString, which provide Unicode strings, and the NSText system in AppKit, which allows the programmer to place string objects in the GUI.

NSText and its related classes are used to display and edit strings. The collection of objects involved permit an application to implement anything from a simple single-line text entry field to a complete multi-page, multi-column text layout schema, with full professional typography features such as kerning, ligatures, running text around arbitrary shapes, rotation, full Unicode support, and anti-aliased glyph rendering. Paragraph layout can be controlled automatically or by the user, using a built-in "ruler" object that can be attached to any text view. Spell checking is automatic, using a system-wide set of language dictionaries. Unlimited undo/redo support is built in. Using only the built-in features, one can write a text editor application in as few as 10 lines of code. With new controller objects, this may fall towards zero. When extensions are needed, Cocoa's use of Objective-C makes this a straightforward task. Objective-C includes the concept of "categories," which allows modifying existing class "in-place". Functionality can be accomplished in a category without any changes to the original classes in the framework, or even access to its source. In other common languages, this same task requires deriving a new subclass supporting the added features, and then replacing all instances of the original class with instances of the new subclass.

Implementations and bindings[edit]

The Cocoa frameworks are written in Objective-C. Java bindings for the Cocoa frameworks (termed the Java bridge) were also made available with the aim of replacing Objective-C with a more popular language[8] but these bindings were unpopular among Cocoa developers and Cocoa's message passing semantics did not translate well to a statically-typed language such as Java.[9] Cocoa's need for runtime binding means many of Cocoa's key features are not available with Java. In 2005, Apple announced that the Java bridge was to be deprecated, meaning that features added to Cocoa in macOS versions later than 10.4 would not be added to the Cocoa-Java programming interface.

At Apple Worldwide Developers Conference (WWDC) 2014, Apple introduced a new programming language named Swift, which is intended to replace Objective-C.[10]

AppleScriptObjC[edit]

Originally, AppleScript Studio could be used to develop simpler Cocoa applications.[11] However, as of Snow Leopard, it has been deprecated. It was replaced with AppleScriptObjC, which allows programming in AppleScript, while using Cocoa frameworks.[12]

Other bindings[edit]

The Cocoa programming environment can be accessed using other tools with the aid of bridge mechanisms such as PasCocoa, PyObjC, CamelBones, RubyCocoa, and a D/Objective-C Bridge.

Third-party bindings available for other languages include AppleScript, Clozure CL, Monobjc and NObjective (C#), Cocoa# (CLI), Cocodao and D/Objective-C Bridge,[13][14] LispWorks, Object Pascal, CamelBones (Perl), PyObjC (Python), FPC PasCocoa (Lazarus and Free Pascal), RubyCocoa (Ruby).[15]

A Ruby language implementation named MacRuby, which removes the need for a bridge mechanism, was formerly developed by Apple, while Nu is a Lisp-like language that uses the Objective-C object model directly, and thus can use the Cocoa frameworks without needing a binding.

Other implementations[edit]

There are also open source implementations of major parts of the Cocoa framework, such as GNUstep and Cocotron,[16] which allow cross-platform Cocoa application development to target other operating systems, such as Microsoft Windows and Linux.

See also[edit]

References[edit]

  1. ^ a b Mac Technology Overview: OS X Frameworks. Developer.apple.com. Retrieved on September 18, 2013.
  2. ^ Amit Singh (June 19, 2006). Mac OS X Internals: A Systems Approach. ISBN 0-321-27854-2. Cocoa is an important inheritance from NeXT, as indicated by .. the "NS" prefix
  3. ^ Mardesich, Jodi (April 14, 1997). "A Sour Note in Apple's Rhapsody Once-Loyal Software Writers Wary of New OS as Crucial Conference Looms". No. Morning Final. San Jose Mercury News. Archived from the original on March 6, 2016. Retrieved August 13, 2015.
  4. ^ Apple Inc. (May 10, 1999), WWDC 1999, retrieved April 27, 2024
  5. ^ HybridWorld. Cilinder.be. Retrieved on September 18, 2013.
  6. ^ Wikibooks - Some Objective-C advantages
  7. ^ "Key-Value Coding Programming Guide". Retrieved September 27, 2021.
  8. ^ Steve Klingsporn (2003). "Writing Cocoa Apps in Java". MacTech. 19 (12).
  9. ^ "Using the Java Bridge" (PDF). Apple Inc. Because Java is a strongly typed language, it requires more information about the classes and interfaces it manipulates at compile time. Therefore, before using Objective-C classes as Java ones, a description of them has to be written and compiled.
  10. ^ "Apple's top secret Swift language grew from work to sustain Objective-C, which it now aims to replace".
  11. ^ "AppleScript Studio Programming Guide (Not Recommended): About AppleScript Studio". Apple, Inc. Retrieved November 20, 2013.
  12. ^ "AppleScriptObjC Release Notes". Apple, Inc. Retrieved November 20, 2013.
  13. ^ Cocodao, bridge to create Cocoa applications in D language.
  14. ^ D/Objective-C Bridge, a language binding mechanism for Cocoa.
  15. ^ more extensive list of implementations
  16. ^ Cocotron, free software implementation of Cocoa.

Bibliography[edit]

External links[edit]