Thursday, September 18, 2014

#26 2012-05-07 01:08:44 pm

fiz
Member
From: Lausanne, Switzerland
Registered: 2011-03-02
Posts: 849

Re: Translate ASOC into Objective-C?

StefanK wrote:

Consider also that using arrayWithContentsOfFile the array items are not mutable although the array itself is mutable.

Ok, so I tried this without success:

self.classNames = [NSMutableArray initWithArray: [theDictionary allKeys] copyItems : true];

Xcode says: No known class method with such name. I *know*, but why can I not use an instance method?

Now take time to look back at ASOC and consider that

        set my classNames to theDictionary's allKeys()

simply works-- elements of the AS list are mutable, no worries,  and the code is much more readable, is not it?

Last edited by fiz (2012-05-07 01:10:11 pm)


Bernard

Offline

 

#27 2012-05-07 01:22:45 pm

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

Re: Translate ASOC into Objective-C?

you forgot the alloc

Applescript:

self.classNames = [[NSMutableArray alloc] initWithArray: [theDictionary allKeys] copyItems : YES];

but copying the items doesn't make them mutable, only new objects are created.

The mutable issue is:

If you create an mutable array with [NSMutableArray arrayWithArray:someArray], the new array is mutable that means you can add, insert and remove items.
If one of the items in the array is an dictionary for example and this dictionary was mutable in someArray, it's now immutable.


regards

Stefan

Offline

 

#28 2012-05-07 04:47:22 pm

fiz
Member
From: Lausanne, Switzerland
Registered: 2011-03-02
Posts: 849

Re: Translate ASOC into Objective-C?

Ok, and now… what the heck is this:

@synthesize gSegControl = _gSegControl;

a) is the property gSegControl "renamed" _gSegControl? And why?
b) what's the difference between self.gSegControl and _gSegControl
c) which one should I use? which one should I not? roll

Thanks!


Bernard

Offline

 

#29 2012-05-07 05:13:01 pm

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

Re: Translate ASOC into Objective-C?

first of all

fiz wrote:

No, my real problem is code readability

mine too, gSegControl is not very readable wink

fiz wrote:

@synthesize gSegControl = _gSegControl;

a) is the property gSegControl "renamed" _gSegControl? And why?!

there is an instance variable _gSegControl and a property gSegControl.
An instance variable can be synthesized with a different name

fiz wrote:

b) what's the difference between self.gSegControl and _gSegControl!

the dot syntax self. uses the synthesized accessor method, the second case accesses the instance variable directly.

fiz wrote:

c) which one should I use? which one should I not? roll!

for key value observing/coding use the property with self. syntax, otherwise the instance variable is sufficient.

Normally it's not necessary to synthesize a property with a different name,
if you don't know the purpose it confuses more than it helps


regards

Stefan

Offline

 

#30 2012-05-07 06:34:05 pm

fiz
Member
From: Lausanne, Switzerland
Registered: 2011-03-02
Posts: 849

Re: Translate ASOC into Objective-C?

Ok… so let's say I want to reproduce :


script myAppDelegate
    property parent : class "NSObject"
    property theSegmentedControl : missing value -- outlet
    property theArrayController : missing value -- outlet
    property theDictionary : missing value  -- no outlet, just a property to be accessed by all methods
    property currentClass : missing value -- no outlet, just property

I should declare, in the .h file :

@interface myAppDelegate : NSObject <NSApplicationDelegate>

@property (assign) IBOutlet NSSegmentedControl *theSegmentedControl;
@property (assign) IBOutlet NSArrayController *theArrayController;
@property (assign) NSMutableDictionary *theDictionary;
@property (assign) NSString *currentClass;

@end

and then, in the .m file :

@implementation myAppDelegate

@synthesize theArrayController;
@synthesize theSegmentedControl;
@synthesize theDictionary;
@synthesize currentClass;

And now for coding:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Classes" ofType:@"plist"];

    theDictionary = [[NSMutableDictionary alloc] dictionaryWithContentsOfFile: filePath];
// NO: gives no visible @interface for 'NSMutableDictionary declares the selector 'dictionaryWithContentsOfFile'

    theDictionary = [NSMutableDictionary dictionaryWithContentsOfFile: filePath];
// NO: gives EXC_BAD_ACCESS -- Bang!

    self.theDictionary = [[NSMutableDictionary alloc] dictionaryWithContentsOfFile: filePath];
// NO: gives error no visible @interface…

And of course    self.theDictionary = [NSMutableDictionary dictionaryWithContentsOfFile: filePath]; bangs too…

Remember? I'm trying to translate only a working property theDictionary : missing value! Maybe I'm too stupid but I just can't see yet the advantages of Objective-C… How long did I read on another post? One year? sad


Bernard

Offline

 

#31 2012-05-07 06:55:20 pm

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

Re: Translate ASOC into Objective-C?

StefanK wrote:

Unlike Shane's statement I recommend to always retain (for collections) or copy (for NSString/NSNumber etc) object properties even in garbage collection environment and set them with self. to nil in the dealloc method.

At one stage, using retain generated an error at compile time. The advice was clearly to use assign (or copy). But the latest I can find is a message from one of the runtime engineers: "(retain) and (assign) are identical under GC."

But dealloc has never been supported under GC.


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

Offline

 

#32 2012-05-07 07:08:18 pm

rdelmar
Member
From: Sonoma County, CA
Registered: 2010-03-05
Posts: 1008

Re: Translate ASOC into Objective-C?

theDictionary = [[NSMutableDictionary alloc] dictionaryWithContentsOfFile: filePath];

You don't use alloc unless the method that follows starts with init.  dictionaryWithContentsOfFile: has the alloc built in. 

theDictionary = [NSMutableDictionary dictionaryWithContentsOfFile: filePath];

You should use self.theDictionary although I don't think what you did should give you a bad access error.  Did you try logging filePath to see if it's giving you a good path?

Ric

Offline

 

#33 2012-05-07 07:14:46 pm

fiz
Member
From: Lausanne, Switzerland
Registered: 2011-03-02
Posts: 849

Re: Translate ASOC into Objective-C?

Hi Ric,

The file path (and the file itself) are correct.
if I
    NSLog(@"Dic %@ ", self.theDictionary);
I get

2012-05-08 02:14:06.271 testObjC[26709:403] Dic Dic 
(lldb)

then bang.


Bernard

Offline

 

#34 2012-05-07 07:47:25 pm

rdelmar
Member
From: Sonoma County, CA
Registered: 2010-03-05
Posts: 1008

Re: Translate ASOC into Objective-C?

Bernard,

I can't see what's wrong with what you have -- you have my email, why don't you send me a copy of your testObjC project, and I'll have a look at it.

Ric

Offline

 

#35 2012-05-08 05:35:16 pm

fiz
Member
From: Lausanne, Switzerland
Registered: 2011-03-02
Posts: 849

Re: Translate ASOC into Objective-C?

Ok, I got from Ric a working solution. In fact, one should know what are ASOC properties equivalents in ObjC. The docs are unclear on this matter, because scripters just have to declare:

property myButton : missing value -- for outlets
property myArray : {} -- for other data (also bindable in IB)

In Objective-C this should be:

@property (assign) IBOutlet NSSegmentedControl *theSegmentedControl ; // for outlets
@property (retain) NSMutableDictionary *theDictionary ; // for other properties (dics, arrays and so on)
@property (copy) NSString *currentClass ; // for strings

No need to deallocate "by hand".

We had always this problem with the old Memory Manager of the Toolbox, we prevented "handles" (pointers to pointers) to move by freezing them into memory to dereference them safely, and we had to unlock them and free them "manually". As I read, the Garbage Collector is reliable, and to send messages to nil objects does not make you restart your Mac anymore… --remember the "bomb" icon? wink

Happy coding!


Bernard

Offline

 

#36 2012-05-08 05:42:38 pm

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

Re: Translate ASOC into Objective-C?

Bernard,

See my post above -- assign and retain are identical under garbage collection -- they both do an assign (you can't do a retain in GC).


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

Offline

 

#37 2012-05-08 05:45:34 pm

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 1906

Re: Translate ASOC into Objective-C?

The problem is using an autorelease object without using an local autorelease pool. The error can happen anywhere on the fly because the object is added to a pool in another scope. As soon as the 'unknown' pool receives a drain the dictionary is freed. So use alloc->init or new methods just like most of Apple's example code and use retain->release or turn garbage collector on when you start with Objective-C. It makes the start a bit easier


Kind regards

Offline

 

#38 2012-05-08 06:05:49 pm

fiz
Member
From: Lausanne, Switzerland
Registered: 2011-03-02
Posts: 849

Re: Translate ASOC into Objective-C?

Well, I don't want to play on thin ice. What is best?

Compiler LLVM GCC 4.2 with Garbage Collection "supported/required"
or
Compiler LLVM compiler 3.1 with Garbage Collection "unsupported" ?

Last edited by fiz (2012-05-08 06:06:13 pm)


Bernard

Offline

 

#39 2012-05-08 06:25:29 pm

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 1906

Re: Translate ASOC into Objective-C?

fiz wrote:

@property (assign) IBOutlet NSSegmentedControl *theSegmentedControl ; // for outlets
@property (retain) NSMutableDictionary *theDictionary ; // for other properties (dics, arrays and so on)
@property (copy) NSString *currentClass ; // for strings

I hope you're not trying to share your Objective-C knowledge already with us when you just start. Like Shane mentioned the properties behaves differently with GC on or off. please don't share bad/incomplete information. Assign, copy and retain are used in different situations. Otherwise Apple wouldn't give us a choice, you're not in the ASOC world anymore. Sometimes I want to use assign while the next time I want to use copy or retain. With autorelease I can use a retain without any problem but when no autorelease is used and GC is turned off I know there is a high change of multi retains and will cause leaks. In those cases I use copy, assign or write the get and set myself.

As far for a compiler. In basic terms it only adds some support for changes to a programming language and most of the changes are compiler instructions so I would go for GCC 4.2 (i use it myself). GC is just some machine code that is added to your compiled code. It also runs in 1 - 4 threads next with your code (that's why you application loses 10% performance in general).


Kind regards

Offline

 

#40 2012-05-08 06:50:22 pm

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

Re: Translate ASOC into Objective-C?

If you are keeping any ASObjC code, in practice you need to use garbage collection. That may change in future; it's been publicly stated that garbage collection will be deprecated in 10.8.

The whole issue of memory management is a thorny one. Long-time Objective-C programmers are comfortable with retain/release, and many don't see the need to change. Garbage collection looks doomed (sadly, IMO). ARC (automated reference counting) looks like the way of the future, and arguments about factory methods versus init/new will largely become moot.

For someone starting out like you at the moment, that's not much comfort...

The supported/required distinction is really for frameworks that will be used in other projects.

I don't think the compiler will make any difference for you, although the LLVM compiler looks to be the way of the future.


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

Offline

 

#41 2012-05-08 07:04:45 pm

fiz
Member
From: Lausanne, Switzerland
Registered: 2011-03-02
Posts: 849

Re: Translate ASOC into Objective-C?

I hope you're not trying to share your Objective-C knowledge already with us when you just start.

Thanks to remind me that, I almost forgot… hmm By the way, I didn't find this myself, I just trying to understand how all this work.

So I'll use this compiler (it's not the default one) with garbage collection required. I get one warning:

cc1obj: warning: -Wuninitialized is not supported without -O

which gets away if I change the optimization level (for debug). Is that ok?


Bernard

Offline

 

#42 2012-05-08 07:12:26 pm

fiz
Member
From: Lausanne, Switzerland
Registered: 2011-03-02
Posts: 849

Re: Translate ASOC into Objective-C?

Shane Stanley wrote:

I don't think the compiler will make any difference for you

No, you're right, I'm not smart enough to see the differences (and probably I'll never be), but if I don't set all options accordingly, the project won't build… and *that* makes a difference. wink


Bernard

Offline

 

#43 2012-05-08 08:01:41 pm

rdelmar
Member
From: Sonoma County, CA
Registered: 2010-03-05
Posts: 1008

Re: Translate ASOC into Objective-C?

The project that Bernard sent to me was using ARC, not GC, and the use of assign for the NSMutableDictionary was what was causing his app to crash.  Since he mentioned that he was interested in iOS as well as OS X, I think that ARC is the way to go -- it certainly seems like the way Apple is pointing us.  It is my understanding that ARC takes care of pretty much all the memory management as long as you stick to cocoa ( but core foundation objects are a different matter).  If this isn't true, I hope someone will enlighten me.

As far as retain, assign or copy, it seems like (from what I've seen) that as a general rule of thumb, you want to use retain for objects other than strings, where you use copy, and assign for IBOutlets and primitive types.  I know this isn't always true, but for someone starting out in Objective-C, it's a place to start.  I don't even know whether we should still be using retain and assign, it looks like Apple is moving to strong and weak.

Ric

Offline

 

#44 2012-05-08 08:17:34 pm

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

Re: Translate ASOC into Objective-C?

rdelmar wrote:

It is my understanding that ARC takes care of pretty much all the memory management as long as you stick to cocoa ( but core foundation objects are a different matter).

It takes care of most of the mundane stuff, but you still effectively have responsibility with things like properties.


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

Offline

 

#45 2012-05-09 04:01:14 am

DJ Bazzie Wazzie
Member
From: the Netherlands
Registered: 2004-10-20
Posts: 1906

Re: Translate ASOC into Objective-C?

rdelmar wrote:

T... I think that ARC is the way to go -- it certainly seems like the way Apple is pointing us.  It is my understanding that ARC takes care of pretty much all the memory management as long as you stick to cocoa ( but core foundation objects are a different matter).  If this isn't true, I hope someone will enlighten me.

You're right.

Properties in projects/files with arc should be defined the same way as other non GC projects. Performance-wise ARC doesn't drop performance like GC but arc is more statically while GC is more dynamic. GC had a big advantage with objects that are created at run time like ASOC. The big difference is that ARC is a pre compiler while GC works at runtime. The only thing I don't like about ARC is that it behaves like a zombie (read:stupid). I mean It adds release and retains for you, it could also look if the coder has added a retain or release already but it doesn't. Another dislike is that I'm not allowed to create C structure anymore with pointers to Objective-C objects. For tool free bridge types (you named core foundation objects) needs a special casting. ARC indeed only applies for objects that have an retain counter (Objective-C objects). In other words for lightweight high level applications arc can be useful but a lot of developers needs to work with core foundation types (which are faster).

However I like the @autorelease{....} because it makes your code better readable, increase performance and best of all you don't need to have ARC enabled to use this. But I name this because it is introduced with ARC.


Kind regards

Offline

 

#46 2012-05-09 08:14:12 am

fiz
Member
From: Lausanne, Switzerland
Registered: 2011-03-02
Posts: 849

Re: Translate ASOC into Objective-C?

Thank you for all these informations!

… So, I'd like to have confirmation that I have understood the basics:

For ASOC projects, GC ON. No worries about Properties.

For ObjC projects, it remains unclear (sorry). There are a number of build options, certain seem to work, others produce run-time error, others produce fatal errors. These options, sometimes mutually exclusive, are:

a) the choice of the compiler
b) the CG options [unsupported/supported/required]
c) code optimization level
d) ARC

These settings, according to what I have read from you, seem to determinate how to set property attributes.

Now please, as a beginner in ObjC, I don't want to generate debates on what is the best solution for a specialist working with C or core foundation, I would just ask for the best settings for:

- be reasonably sure that my properties will behave like ASOC properties did.
- be reasonably sure that my unused arrays, temporary variables and so on will be deallocated automatically -- not too fast, not too late. So with objects built on the fly.
- have a reasonably fast code.
- follow Apple's recommendations for the future.

considering:

- my apps may be complex but generally handle small amounts of data (arrays, dictionaries max. 30000 entries)
- I don't have to develop for pre-OS 10.7 (all our computers will run under Lion in 2 months)

Every hints are very, very welcome!

Regards,


Bernard

Filed under: asoc, objc, settings, compiler

Offline

 

#47 2012-05-09 08:28:19 am

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

Re: Translate ASOC into Objective-C?

Again I'd like to recommend to learn the language.

At least read Apple's The Objective-C Programming Language
Look also into the programming guides for the basic Foundation classes (string, number, the collections, date, url)

Personally I read also these books, they were and still are very helpful:
Cocoa Programming for Mac OS X by Aaron Hillegass
Programming in Objective-C by Stephen Kochan.


regards

Stefan

Offline

 

#48 2012-05-09 08:39:55 am

fiz
Member
From: Lausanne, Switzerland
Registered: 2011-03-02
Posts: 849

Re: Translate ASOC into Objective-C?

Thank you Stefan

The Objective-C Programming Language is already in my iPad's iBooks, I read it every night… but what I like is to read, test, read again, test again. I just wanted to get Xcode configured to play with these new notions…

For the moment I have the same tiny "application" (half a dozen of properties, four or five handlers and a basic interface with a window, some controls and table views)… my more complex line is

classNames = [NSMutableArray arrayWithArray: [[theDictionary allKeys]sortedArrayUsingSelector:@selector(compare:)]];

So really the basics. May I go with the defaults settings?

Last edited by fiz (2012-05-09 08:42:30 am)


Bernard

Offline

 

#49 2012-05-09 08:48:00 am

t524ube
Member
Registered: 2009-09-29
Posts: 154

Re: Translate ASOC into Objective-C?

Could anyone recommend an Objective-C book that is easy to understand in the same way Shane Stanley did with his great book.  That's of course provided that the individual has programing ability.

I like ASOC as it allows me to write simple apps that I can use at work.  but if I can learn OC that will be a plus provided I have the time to dedicate to it.

Offline

 

#50 2012-05-09 08:55:13 am

fiz
Member
From: Lausanne, Switzerland
Registered: 2011-03-02
Posts: 849

Re: Translate ASOC into Objective-C?

I just had a look on Kochan's book and its approach is non-C programming big_smile . I will personally follow Stefan's hint and load these books on my iPad.


Bernard

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.043 seconds, 10 queries executed ]

RSS (new topics) RSS (active topics)