Lost in contextInfo, passing scalar parameter…

Hello,

I don’t understand how to pass a parameter in the contextInfo of a sheet alert.

An alert is deployed, which may be

  • “terminating”, and then I want it to close the document after OK is clicked
  • “not terminating”, and let it opened after dismissing with OK.

So I call (for example): [self userAlert: @“fatalErr” terminating: YES];

This is the code:

I’m missing something elementary here. Thank for your help.

Hi,

two problems:
¢ contextInfo is supposed to be a void pointer to an object

  • the contextInfo object isn’t retained automatically.

A workaround is to pass a string constant, which isn’t affected by the retain counter.

btw: the correct boolean type in ObjC is the capitalized BOOL

Try this


- (void) userAlert: (NSString*) withKey isTerminating: (BOOL )terminating {
    NSAlert *theAlert = [NSAlert alertWithMessageText:[[player selection]valueForKey:withKey]
                                        defaultButton:nil alternateButton:nil otherButton:nil
                            informativeTextWithFormat:@""];
    
    [theAlert beginSheetModalForWindow:mainWindow
                         modalDelegate:self
                        didEndSelector:@selector(alertDidEnd:returnCode:contextInfo:)
                           contextInfo:(terminating) ? @"YES" : @"NO"];
    
    NSLog(@"Passed %x", terminating);       // gives the correct value
}

- (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
    
    NSString *context = (NSString *)contextInfo;
    NSLog(@"Info %@", context);       // gives zero.?
    if ([context isEqualToString:@"YES"]) [self close];
}


Thanks Stefan. It works. Don’t forget the

NSString *context = ([b]__bridge[/b] NSString *)contextInfo;

if you’re using ARC (as I do).

I don’t :slight_smile:

I don’t use Arc either, as I guess it is better to manage memory manually for speed, and trust the build an analyse, to find any flaws in my code, which have worked well so far.

But it would still be interesting to know how to enable it. I’m still on XCode 3.2 :slight_smile:

FYI, the claim is that ARC is actually faster, because it removes many redundant retain/release/autorelease calls.

I’m pretty sure it requires Xcode 4 – it certainly requires the use of Apple’s LLVM compiler.

Ahh. Faster is always better! :slight_smile: And as long as it isn’t going to run on an iPhone.

And then I can also remove the tests to avoid the redundant calls then, apart from avoiding the unavoidable.

I’ll give Arc a close look.

Maybe it is time to upgrade! LLVM sounds good as well. (Though I thought I had it. )

Hello!

I am investigating Xcode and what it can do for me at the moment! I do have GCC 4.2 and I can set “Use garbage collection” from the build settings when I scroll the list in build settings.