Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For the benefit of posterity, I'll share the solution I ended up using. At a high level, the solution is to compile the resource in question into the application binary, thus obviating the need to also copy it to bundle resources. </p> <p>I decided a generic and reliable way to compile any file data into the binary would be to store the file contents in a static byte array in a header file. Assuming there is already a header file created and added to the static lib target, I made the following bash script to read a file, and write its contents as a byte array of hex literals with C syntax. I then run the script in "Run Script" build phase <em>before</em> the Compile Sources and Copy Headers build phases:</p> <pre><code>#!/bin/bash # Hexify.sh reads an input file, and hexdumps its contents to an output # file in C-compliant syntax. The final argument is the name of the array. infile=$1 outfile=$2 arrayName=$3 fileSize=$(stat -f "%z" $infile) fileHexString=$(hexdump -ve '1/1 "0x%.2x, "' $infile) prefix=$arrayName suffix="Size" variableName=$arrayName$suffix nullTermination="0x00" echo "//" &gt; $headerFile echo "// This file was automatically generated by a build script." &gt;&gt; $headerFile echo "// Do not modify; the contents of this file will be overwritten on each build." &gt;&gt; $headerFile echo "//" &gt;&gt; $headerFile echo "" &gt;&gt; $headerFile; echo "#ifndef some_arbitrary_include_guard" &gt;&gt; $headerFile echo "#define some_arbitrary_include_guard" &gt;&gt; $headerFile echo "" &gt;&gt; $headerFile echo "static const int $variableName = $((fileSize+1));" &gt;&gt; $outfile echo "static const char $arrayName[$variableName] = {" &gt;&gt; $outfile echo -e "\t$fileHexString$nullTermination" &gt;&gt; $outfile echo "};" &gt;&gt; $outfile echo "#endif" &gt;&gt; $headerFile </code></pre> <p>So, for example, if I have a resource file example.txt:</p> <pre><code>Hello this is a file </code></pre> <p>And I were to run <code>./Hexify.sh example.txt myHeader.h exampleArray</code>, the header would look like this:</p> <pre><code>// // This file was automatically generated by a build script. // Do not modify; the contents of this file will be overwritten on each build. // #ifndef some_arbitrary_include_guard #define some_arbitrary_include_guard static const int exampleArraySize = 21; static const char exampleArray[exampleArraySize] = { 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x74, 0x68, 0x69, 0x73, 0x0a, 0x69, 0x73, 0x20, 0x61, 0x20, 0x66, 0x69, 0x6c, 0x65, 0x00 }; #endif </code></pre> <p>Now, at any point in time that I would have loaded said resource from the main bundle, I can instead refer to the byte array in that header file. Note that my version of the script adds a null terminated byte, which makes the data suitable for creating string objects. That may not apply in all cases.</p> <p>As one final addendum, I apologize if that bash script makes any real bash programmers cringe; I have almost no idea what I'm doing with bash.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload