What does that line mean in ObjectScript? - intersystems-cache

This is a line in class MonCache.DB of this project:
s fieldsNames = ##class(MonCache.Types.Object) modificationOperator.getNames()
It is the first time I see that... Is that a cast, by any chance?

It's method casting and whitespace does not matter in this particular context.

Related

can you return a b2Fixture?

Have a function that returns a function and tried to call it like this
_spriteFixture = [self addBoxBodyToSprite:sprite];
However I keep getting an error message stating
HelloWorldLayer.mm:174:20: Assigning to 'b2Fixture *' from incompatible type 'b2Fixture'
my _spriteFixture is a b2Fixture and is declared in the header file. Any ideas what this means or is there a better way to return the fixture?
Thanks
You may be hitting that C++ problem where the compiler tries to be helpful by implictly calling your constructor (and then a destructor later). Tag your constructors as "explicit" to see if that helps.
For any one that wants to know, my method wasnt set to return a pointer -(b2Fixture*)methodName Instead of -(b2Fixture)methodName –

Cocoa style guidelines for an init method with BOOL variable?

I have a data class that starts with a bool value:
- (id)initWithBool:(BOOL)hasSomeValue anotherVariable:(var type)var ...
But this is unreadable when instantiating in XCode, because "hasSomeValue" is not shown when auto-complete appears.
-(id)initWithHasSomeValue:(BOOL)hasSomeValue anotherVariable:(var type)var ...
Is also rather clunky. I've done some searching but can't seem to find Apple's recommended specs on this.
EDIT: A pointer to an example by Apple would help very much. I still haven't managed to find one.
You'd really want something that tells you WHAT the BOOL actually represents
- (id)initWithBool:(BOOL)hasSomeValue ...
- (id)initWithHasSomeValue:(BOOL)hasSomeValue ...
would be comparable to
- (id)initWithString:(NSString*)string ...
if you were initiating a BOOL (which clearly is nonsense), except with [NSNumber numberWithBool:]
Generally speaking, the name of the parameter should describe its semantics, which "bool" doesn't really do. E.g. "initWithName:" is better than "initWithString:".
Even more generally speaking, you have a data class that takes a boolean as it's first parameter? Maybe you need to rethink why it's there at all?
Have you added the custom init method to your header file?
Although it will compile (with a warning) and run fine, the custom init method will only appear in auto complete if it appears in the header file.

attempt to mutate immutable object randomly thrown

One part of the program takes text from a uitextfield, copies it to a mutable string and then performs
sharedManager.ce_name=name.text
[sharedManager.ce_name replaceOccurrencesOfString:#" " withString:#"%20"
options:NSLiteralSearch range:NSMakeRange(0, [sharedManager.ce_name length])];
At this point it always gave me "attempt to mutate immutable object" - it was not random
The first time I got this error I changed it to
sharedManager.ce_name=(NSMutableString *)name.text
This STILL gave me the attempt to mutate immutable object error, but it would occur randomly - weird right?
I then changed it to
NSMutableString *mutable_name = [NSMutableString stringWithString:name.text];
sharedManager.ce_name=mutable_name;
It has yet to fail on me doing it this way but I am convinced that I have not found the solution.
my questions:
1) Could that fact that it was doing it randomly after the first fix indicate I have some deep seated memory management problem?
2) Why didn't the C-style cast fix it?
3) Will my current fix work?
Thanks for your time :)
The problem here is the way in which you're using casting. When you cast a pointer, it just starts treating the memory at that location as though it were a block of data representing that type.
So if I've got a pointer to a Car class: Car* mycar;, and I cast it to a Person object: (Person*)mycar;, the program will try to access the memory there as though it was pointing at a Person object. However, a car is not a person, except in an old TV sitcom, so when it tries to access members on this or call functions, it's going to try to go to a memory location that contains something other than what it's expecting, and undefined things happen (generally, crashing).
NSMutableString and NSString are incompatible in this regard, and so casting from one to the other will result in terrible times. Your fix ([NSMutableString stringWithString:]) is the correct solution.
If it was doing it randomly, it means that name.text was sometimes a mutable string and some times an immutable string.
Casting between objects like that doesn't change the class of the object. It won't make your immutable object mutable.
That "fix" is probably the best way to do it (at least from what I can see in the code you are showing)
Without seeing at least the declarations of the variables involved, it's difficult to say for certain, but your final solution, creating a new mutable string is likely the correct fix. As for your questions,
Not memory management per se, but it was probably overwriting something it shouldn't have somewhere.
Casts cannot change the fundamental type of an object. You had (presumably) an NSString and all the casting in the world cannot make it into an NSMutableString.
Like I said, probably, but we'd need to see more code to make sure. It's certainly a much better fix.

How to get rid of warnings when using Category in XCode

in my code I have created a Category over UIViewController, so that every of my UIViewControllers has a error handling method. Unfortunatey now whenever I call this method from the category I get the following warning in XCode:
'MainWindowViewController' may not respond to '- (...method name...):'
We try to have our code without any warnings, so I wonder if there is any clever way to keep the Category and get rid if the "may not respond to" warning".
Thanks for your help!
Importing the header where your category is declared to implementation file where methods from that category are used should eliminate that warning.
#Vladimir is right , you need to import the header file in your implementation class.
There could be one more reason for the warning you get during compilation of your code,
if you don't declare the method in header file but implement the in implementation file
Vladimir is, of course, correct. Adding a category to class makes those functions available to all instances of that class whether the header file is #imported or not. Objective-C is a dynamic language.
However - the compiler is warning you that it can't see the declaration of those messages at compile time. The code could still be valid; which is why it raises a warning rather than an error.
I like to import the category into a class that requires the extensions that the category provides. I find it a useful way of reminding me of the dependency. Some programmers, however, think that since a category provides its methods to all instances of the class, it's pointless to add it to just one class.
If you prefer not to import the category to each class that uses it, but you would like to have clean compiles - #import the category header into the pch file instead.
Try restarting XCode and cleaning. I had the same problem, was definitely including the header file but still got the warning. Restart and clean fixed it.
When everything else sensible fails to fix the problem retype the offending call on a new line just below the offending line.
You might find that the complier is happy with the new line. Delete the old line. (Things that make you go HMMM?)
I've found this happens on more than one occasion with xcode (and other editors). When all else fails type the line again.

Don't have the right syntax when comparing strings in iPhone

I am trying to compare the following values:
gType = [[UILabel alloc]init];
if (gType = [NSString string:#"BUSINESS"]) {
I get a warning that 'NSString' may not respond to '+string:'
I am unsure what is wrong. gType is a value that I populate from a db query. Other text values from the same query show up fine in a UITableView, so I am pretty confident I have created it properly.
thx,
Your code is calling the "String" class method on the NSString class. This doesn't accept any arguments, which is your problem here.
The correct way to write your code would be something like:
if ([gType.text isEqualToString:#"BUSINESS"])
For starters, = is the assignment operator in C and does not compare anything. Secondly, even if you were using a comparison operator there, you'd be comparing pointer addresses, not the textual contents of the objects.
Read this
You're looking for:
if ([someString isEqual:#"Something else"]) { ... }
As NSD said, you have a few fundamental problems with your code there.
If you want to compare strings in Cocoa Touch, you can use the -isEqualToString: method on NSString.