SecItemCopyMatching返回errSecItemNotFound

时间:2015-10-13 01:13:29

标签: ios swift keychain

与REST API通信并在iOS的钥匙串中保存API令牌。但是钥匙串代码会引发nil错误。

KeychainAccess.swift:

public class func passwordForAccount(account: String, service: String = "keyChainDefaultService") -> String? {
    let queryAttributes = NSDictionary(objects: [secClassGenericPassword(), service, account, true], forKeys: [secClass(), secAttrService(), secAttrAccount(), secReturnData()])

    var retrievedData: NSData?
    var extractedData: AnyObject?
    let status = SecItemCopyMatching(queryAttributes, &extractedData)

    if (status == errSecSuccess) {
        retrievedData = extractedData as? NSData
    }
    let password = NSString(data: retrievedData!, encoding: NSUTF8StringEncoding)

    return (password as! String)
}

在上面的代码中,retrievedData为零。如果我print(status),我会-25300。从视图控制器调用此函数:

// check if API token has expired
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let userTokenExpiryDate : String? = KeychainAccess.passwordForAccount("Auth_Token_Expiry", service: "KeyChainService")
let dateFromString : NSDate? = dateFormatter.dateFromString(userTokenExpiryDate!)
let now = NSDate()

我不确定我在哪里出错了。有什么指针吗?

1 个答案:

答案 0 :(得分:0)

public class MineWalker {
enum Spaces {Mine,Empty,Player,IceCream};
public static final int BOARD_SIZE = 10;
public final static double MINE_PERCENT = 0.1;

public static void main(String[] args) {
    // TODO Auto-generated method stub
    //The player's location
    int pX = 0;
    int pY = 0;
    //The target location
    Random r = new Random();
    int tX = r.nextInt(BOARD_SIZE);
    int tY = r.nextInt(BOARD_SIZE);
    //User input
    Scanner keyboard = new Scanner(System.in);
    //Set up the board
    Spaces[][] board = new Spaces[BOARD_SIZE][BOARD_SIZE];
    //Initialize the board
    for(int y=0;y<board.length;y++)
    {
        for(int x=0;x<board[y].length;x++)
        {
            board[x][y] = Spaces.Empty;
        }
    }
    board[pX][pY] = Spaces.Player;
    //Puts the goal on the board
    board[tX][tY] = Spaces.IceCream;
    int mines = (int)(BOARD_SIZE * BOARD_SIZE * MINE_PERCENT);
    System.out.println("Welcome to Mine Walker.  Get the ice cream cone and avoid the mines");
    do
    {
       int x = r.nextInt(BOARD_SIZE - 1) + 1; 
       int y = r.nextInt(BOARD_SIZE - 1) + 1;
       if(board[x][y] == Spaces.Empty)
        { 
            board[x][y] = Spaces.Mine;
            mines--;
        }
    }while(mines > 0); // set mines
    boolean gameOver = false;

    while(gameOver == false)
    {
        //Draw the board
        for (int y = 0; y < board.length ; y++ ) 
        {
            for (int x = 0; x < board[y].length ; x++ ) {
                switch(board[x][y])
                {
                case Empty:
                    System.out.print("_");
                    break;
                case Player:
                    System.out.print("X");
                    break;
                case Mine:
                    System.out.print("!");
                    break;
                case IceCream:
                    System.out.print("^");
                    break;
                default:
                    System.out.print("?");
                break;
                }
            }
            System.out.println(" ");
        }
        System.out.println("Enter either -1,0,1 to move in the x or 9 to quit");
        //Movement in the X direction
        int dX = keyboard.nextInt();
        //Or quit
        if(dX == 9)
        {
            System.out.println("Game over");
            System.exit(0);
        }

        System.out.println("Enter either -1,0,1 to move in the y");
        //Movement in the y direction
        int dY = keyboard.nextInt();

        //Checks to see if the movement is valid
        if(dX <-1 || dX>1)
        {
            System.out.println("Invalid input X");
            dX = 0;
        }
        if(dY <-1 || dY>1)
        {
            System.out.println("Invalid input Y");
            dY = 0;
        }

        board[pX][pY] = Spaces.Empty;
        //Moves the player
        pX+=dX;
        pY+=dY;

        //Makes sure everything is still in bounds
        if(pX < 0)
        {
            pX = 0;
        }
        else if(pX>BOARD_SIZE-1)
        {
            pX = BOARD_SIZE-1;
        }
        if(pY < 0)
        {
            pY = 0;
        }
        else if(pY> BOARD_SIZE-1)
        {
            pY = BOARD_SIZE-1;
        }
        String again;
        if(board[pX][pY]==Spaces.Mine)
        {           
            System.out.println("Boom! Dead!");
            System.out.println("Would you like to play again? \"Yes\" or \"No\"");  
            again = keyboard.nextLine();
            if (again.equalsIgnoreCase("Yes"))
            {
                    gameOver = false;   
            }
            else if (again.equalsIgnoreCase("No"))
            {
                System.out.println("Goodbye!");
                System.exit(0); 
            }
        }
        if(board[pX][pY]==Spaces.IceCream)
        {
            System.out.println("You win!");
            System.out.println("Would you like to play again? \"Yes\" or \"No\"");
        }
        board[pX][pY] = Spaces.Player;  
    }   
}}

尝试这样做,这就是我必须让它运作的方式

相关问题