Tuesday, May 11, 2021

#1 2021-03-24 09:01:25 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 707

4 different ways to initializes allocated object

Lets go back to the school and ask questions from people who really know.

In this ASObjC I have include 4 different ways to initializes allocated object.

Question:

   1. Would a type method have any benefits against instance method with same result. ??
   2. Would a instance method have any benefits against type method with same result. ??
   3. If any code in this example is not to recommended could you explain why. ??

All examples return a empty dictionary.

Regards.

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

(**
* [Type Method]: dictionary
*        + (instancetype)dictionary;
*)

-- Creates an empty dictionary
set theDict to current application's NSDictionary's dictionary()

(**
* [Type Method]: alloc
*        + (instancetype)alloc;
* Returns a new instance of the receiving class.
**
* [Instance Method]: init
*        - (instancetype)init;
*)

-- Initializes a newly allocated empty dictionary.
set theDict to current application's NSDictionary's alloc()'s init()

(**
* [Type Method]: new
*        + (instancetype)new;
**
* Allocates a new instance of the receiving class, sends it an init message,
* and returns the initialized object.
**
* This method is a combination of alloc and init. Like alloc, it initializes the
* isa instance variable of the new object so it points to the class data structure.
* It then invokes the init method to complete the initialization process.
**
* https://developer.apple.com/documentation/objectivec/nsobject/1571948-new
*)

set theDict to current application's NSDictionary's new()

(**
* [Type Method]: allocWithZone
*        + (instancetype)allocWithZone:(struct _NSZone * )zone;
**
* The isa instance variable of the new instance is initialized to a data structure
* that describes the class; memory for all other instance variables is set to 0.
* You must use an init... method to complete the initialization process.
**
* For example:
* TheClass *newObject = [[TheClass allocWithZone:nil] init];
* Do not override allocWithZone: to include any initialization code.
* Instead, class-specific versions of init... methods.
* This method exists for historical reasons; memory zones are no longer used by Objective-C.
*)

-- Returns a new instance of the receiving class.
set theDict to (current application's NSDictionary's allocWithZone:(missing value))'s init()


if you are the expert, who will you call if its not your imagination.

Online

 

#2 2021-03-24 05:10:27 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6628

Re: 4 different ways to initializes allocated object

The last method is pointless, the others are much of a muchness. I'd guess alloc()'s init() might be a smidgeon slower, being two calls.

(In fact, in your example they all mostly pointless for a non-mutable class.)


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2021-03-24 06:33:40 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 707

Re: 4 different ways to initializes allocated object

@Shane

The thought cross my mind when I read from stackoverflow about type method vs instance method
https://softwareengineering.stackexchan … ce-methods

The example was only to show type method and instance method of something and not a test case.

From the same thread from the link they say the difference will not matter in performance.
I have read elsewhere it could.


if you are the expert, who will you call if its not your imagination.

Online

 

#4 2021-03-24 07:53:01 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6628

Re: 4 different ways to initializes allocated object

Fredrik71 wrote:

From the same thread from the link they say the difference will not matter in performance.
I have read elsewhere it could.



It probably doesn't — in actual Objective-C code. ASObjC adds another layer. It's not a matter of instance method vs class method but rather two calls vs one.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2021-03-25 04:45:20 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11746
Website

Re: 4 different ways to initializes allocated object

Shane Stanley wrote:

I'd guess alloc()'s init() might be a smidgeon slower, being two calls.



Usually the convenience class methods call alloc() init() under the hood, too.


regards

Stefan

Offline

 

#6 2021-03-25 05:02:19 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6628

Re: 4 different ways to initializes allocated object

StefanK wrote:

Usually the convenience class methods call alloc() init() under the hood, too.



Yep — I meant when calling it from ASObjC, when it it involves double the work for the bridge.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)