Xcode 2.5 中 10.4 Tiger PPC 的 macOS 应用程序中从 IBAction 访问 NSString 的问题

问题描述 投票:0回答:1

所以这是一个相当模糊的问题,但就这样吧。 我在工作中只能使用一种 Mac(我在高中教编程),那就是旧的 EMac 1.25Ghz。我已经让老虎运行了。我家里还有一台 1Ghz EMac,也运行 Tiger,都安装了 Xcode 2.5。

我的应用程序大部分工作正常,但我发现从 IBActions 写入 NSString 变量时遇到问题。我可以很好地读取字符串并读取和写入整数或布尔值,但每当我尝试写入它们时,如果它们是存在于 IBAction 方法之外的变量,NSStrings 就会抛出 BAD_EXEC_ACCESS 错误。该代码在我家里的 Mac Pro 上运行良好,针对 macOS 14 编译,但在 Tiger 或 Leopard 中则不行。我尝试在 Xcode 3.1 中重新编译该应用程序,但遇到了相同的问题。我正在寻找一两个兽医,他们可以告诉我如何正确设置从 IBAction 访问的 NSString。

这是我的代码:

#import "GameWindow.h"

@implementation GameWindow

// game variables
int gameStage;
int playerType;
int startUpSetting;
bool languageSelect;
int playerPortrait;
bool firstTimeGettingInput;

//strings are char characters

NSString *playerName;
NSString *inputText;
NSString *onGoingDisplayText;


-(void)awakeFromNib{ //init the view here, set text, ect

    
    //define variable values
    
    gameStage = 0; //where we are in the game
    playerType = 0; //who we're playing as
    startUpSetting = 1; //this becomes 0 once start up perameters have been set
    languageSelect = 0; //0 is English, 1 is Japanese
    playerPortrait = 4; //what the players portrait looks like.
    firstTimeGettingInput = true; //has the game just started?
    
    //strings are char characters
    
    playerName = @"Jyoubu"; //player's name
    inputText = @""; //the command or text the player has just typed
    onGoingDisplayText = @""; //what the player has typed so far
}

-(IBAction) textSubmitted: (id) sender
{ //run the code for when text has been submitted via the input area by pressing enter.

    

    //sort out strings



    NSString *introNameString;
    
    NSString *onGoingDisplayText2 = [mainTextView stringValue];//make a temp string of the current string value to stop crashes
    
    inputText = [inputTextView stringValue]; //save the input text
    
    inputText = [inputText lowercaseString];
    
    //const char *tempChar = (char *)[inputText UTF8String]; //convert the input text to all lower case
    NSLog(@"player name: %@", playerName);
    
    if(languageSelect == 0){
        introNameString = [NSString stringWithFormat: @"Greetings %@ %@", playerName, introStringEng];
    }
}
objective-c xcode macos nsstring ibaction
1个回答
1
投票

从 IBAction 访问 NSString 没有任何问题。

问题出在内存管理上。此错误消息 (BAD_EXEC_ACCESS) 意味着您正在尝试访问已释放的对象。

自 ARC(自动引用计数不适用于 Tiger,并且仅从 10.7 Lion 开始正确支持)以来,内存不会自动管理。

https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

https://developer.apple.com/library/archive/releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226

如果程序依赖于 ARC,则它将无法在旧系统中运行(由于访问已解除分配的对象而崩溃)。

您可以像 ARC 之前那样编写手动保留和释放,但这意味着您必须在任何地方手动管理内存。可能,但不好玩。

© www.soinside.com 2019 - 2024. All rights reserved.